LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-19-2005, 08:31 PM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 30
C++:Operations with iterators


Hi,
I have a quick question about iterators in a vector container. I am trying to use an iterator pointing to the element that is *one before* the last in a vector. It seemed quite reasonable to me that I could use
Code:
vec.end()-2
But that doesn't work. It only works when I use:
Code:
vec.begin()+vec.size()-1
Can someone tell me what is wrong with my original method?
Thanks.
 
Old 10-19-2005, 10:24 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
edit: sorry, had java in my head... nevermind.

Last edited by nadroj; 10-19-2005 at 10:26 PM.
 
Old 10-19-2005, 10:36 PM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 47
I'm not sure. Can you show us the code that uses this?
 
Old 10-19-2005, 10:47 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
ya.. some code would be helpful for when you use it.

are you sure its returning the correct result, or one that happens to be one that is similar to what you expect? test it more if not...

how big is the vector? maybe there isnt enough elements in it to subtract 2.

maybe its returning an addition of the iterator returned from begin + the iterator returned from end?

why not just use something like
Code:
vec.at(vec.size() - 2));
 
Old 10-19-2005, 11:26 PM   #5
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
There is quite a bit of code and it'll take me a while to explain to you what is it supposed to do. But...just in general, I wanted to know if these two methods of accessing an iterator are equivalent. I mean they both compile but the first one gives me weird results. I also thought I read somewhere that end() member function is more limited to what it can do then begin(). I don't have any real experience with these functions, just learning about them.

So in general, is there any difference between these two methods or no?
 
Old 10-19-2005, 11:29 PM   #6
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
OK so this is what I read about end() function. It's from Deitel & Deitel (3rd Ed, Ch20, pg 978):

"...The iterator resulting from end() can be used only in an equality or inequality comparision to determine if the "moving iterator"has reached the end of the container. "

I am not exactly sure if I understand the meaning of this though.

Last edited by frankie_DJ; 10-19-2005 at 11:39 PM.
 
Old 10-19-2005, 11:38 PM   #7
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
im not well educated in c++, but am in java, so i might be right, maybe wrong.. but it seems logical:
i think its saying that, you will use the .begin() method to get the iterator to loop from the start to the end of the collection or vector or whatever.. then, after every loop, you will of course check to see if you are at the end of the list or not, so you need to compare to see if it '==' the .end()?



thats my logical guess.

edit:
actually.. i think thats exactly what its for

clicky

Code:
for( theIterator = charList.begin(); theIterator != charList.end(); theIterator++ ) {
     cout << *theIterator;

Last edited by nadroj; 10-19-2005 at 11:41 PM.
 
Old 10-19-2005, 11:43 PM   #8
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
Oh, I undestand that part. I used it that way succesfully. But why *only* for that? And if that's the case, why does it compile when I do something else with it ( end()-2)?

Last edited by frankie_DJ; 10-19-2005 at 11:44 PM.
 
Old 10-19-2005, 11:51 PM   #9
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
you can subtract 2 or add whatever number and id think it would compile.. it would just might then reference some other, irrelevant and inappropriate part of memory.

i think it might be used in the reverse order iterator thats listed on that site, but i didnt check.
why is .begin() used ONLY to retrive the iterator for the firste element? countless things have only 1 purpose
 
Old 10-20-2005, 12:00 AM   #10
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by nadroj

why is .begin() used ONLY to retrive the iterator for the firste element? countless things have only 1 purpose
Not correct! I can use begin() to access any element. For example if I want to acces an iterator pointing to element anywhere in the vector I can go:
Code:
vec.begin()+i
just as I did in the example I asked about:
Code:
vec.begin()+vec.size()-1
Obviously it doesn't work the same with end() but I can't figure out why.
Thanks for your help anyway.
 
Old 10-20-2005, 12:28 AM   #11
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 47
Actually, I just tried to use "vec.end() - 2" and it seemed to work fine:
Code:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> vec;
  for (int i = 0; i <= 4; i++) {
    vec.push_back(i);
  }

  cout << *vec.begin() << endl; // ouputs "0"
  cout << *(vec.end() - 2) << endl; // outputs "3"

  return 0;
}
outputed:
0
3
 
Old 10-20-2005, 03:28 AM   #12
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
spoon thanks for your little piece of code. i played with it a little bit and realized that
vec.begin() + vec.size()-1=0+4-1=3
and
vec.end()-2=4-2=2
don't return the same iterator after all.
i guess i slept through my arithmetics classes when they taught adding.
 
Old 10-20-2005, 03:39 AM   #13
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 47
Quote:
Originally posted by frankie_DJ
spoon thanks for your little piece of code. i played with it a little bit and realized that
vec.begin() + vec.size()-1=0+4-1=3
and
vec.end()-2=4-2=2
don't return the same iterator after all.
i guess i slept through my arithmetics classes when they taught adding.
Actually, my vector had length of 5; but otherwise I guess you're right.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
retreiving a range of iterators with an stl container g_goblin Programming 1 09-01-2005 05:50 PM
Math operations with PHP linuxfond Linux - Newbie 1 04-20-2005 06:58 AM
Aritmethical operations davi_cabral Linux - Newbie 1 09-26-2003 11:04 AM
Modem operations dbeyes Linux - Newbie 1 04-22-2003 06:09 PM
Copy/Paste operations Rex_chaos Linux - General 1 11-03-2001 06:41 AM


All times are GMT -5. The time now is 08:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration