vector iterators vs operator []

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 232
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

vector iterators vs operator []

 
0
  #1
Jun 17th, 2007
I'm trying to solidify my understanding of some vector concepts.

1) interators are "smart access" in that they can keep track of the array
position and size.
2) the access element operator [] is the "dumb" access to the vector.

Also, I was experimenting with ways to dump the contents of a vector into an array of the same size.

Here's how i got it to happen:
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<int> vint;
  6. int input =1 ;
  7.  
  8. int main()
  9. {
  10. cout << "Enter an integer (Enter 0 to exit) ";
  11.  
  12.  
  13. while(input)
  14. {
  15. cin >> input;
  16. vint.push_back(input);
  17. }
  18.  
  19. size_t n = vint.size();
  20. int arr[n];
  21. cout << "the vector content is: ";
  22. for ( int j = 0; j < n; j++)
  23. {
  24. arr[j] = vint[j];
  25. cout << vint[j] << " ";
  26. }
  27. cout << endl;
  28. cout << "The vector size is: " << vint.size() << endl;
  29. cout << "The content of the array is: "<< endl;
  30. for (int j=0; j < n; j++)
  31. cout << arr[j] << " ";
  32. cout << endl;
  33.  
  34.  
  35. return 0;
  36. }

I tried to do direct assignment:

arr=vint // why won't this work? essentially they're both arrays- or so i thought.

Then I got busted trying to do this:
for (it = iterator vint.begin; it < iterator vint.end() ; it++)
arr[it] = *it; // OK, I guess the iterator is a different animal than an integer? Not to mention the possible scoping problem?

It didn't work if I used an actual integer either like this:

arr[x] = *it // where int x is part of a nested loop. I dont understand why this didn't work since I CAN do:

cout << *it // this will provide the dereferenced pointer data

What's the story here?

I sure this all seems twisted to folks who are comfortable with the nuances, but I would appreciate some clarifications.
I learn best by tinkering...
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: vector iterators vs operator []

 
0
  #2
Jun 18th, 2007
  1. #include <vector>
  2. #include <algorithm>
  3. using namespace std ;
  4. int main()
  5. {
  6. vector<int> cntr( 20U, 7 ) ;
  7. int* array = new int [ cntr.size() ] ;
  8. typedef vector<int>::iterator iterator ;
  9. int x = 0 ;
  10. for( iterator it = cntr.begin() ; it != cntr.end() ; ++it, ++x )
  11. {
  12. array[x] = *it ; // ok
  13. // array[it] = *it ; // error, it is not an integral value
  14. }
  15.  
  16. // the easy way
  17. copy( cntr.begin(), cntr.end(), array ) ;
  18. delete[] array ;
  19. }
Last edited by vijayan121; Jun 18th, 2007 at 5:38 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: vector iterators vs operator []

 
0
  #3
Jun 18th, 2007
>> size_t n = vint.size();
>> int arr[n];
This is not supposed to work !

Originally Posted by JRM View Post
I tried to do direct assignment:
arr=vint // why won't this work? essentially they're both arrays- or so i thought.
I donno what you mean by won't work.
But this thing in particular is already answered by Vijayan, it doesn't work because both are of different types (and there is no acceptable implicit conversion available).
E.g. if u assign an int to a float it would work because although they are different types there is an acceptable conversion available. if u assign a char* to n char it won't work. (understandably they are 2 different things altogether).

Originally Posted by JRM View Post
Then I got busted trying to do this:
for (it = iterator vint.begin; it < iterator vint.end() ; it++)
arr[it] = *it; // OK, I guess the iterator is a different animal than an integer? Not to mention the possible scoping problem?
You guessed correctly abt the different animal thing. .
Type of an iterator is T* where T is the type for which you've specialized the template class vector.
So in your case type of iterator would be int*
There is no subscript operator defined to take int* as index so you're busted.

Originally Posted by JRM View Post
It didn't work if I used an actual integer either like this:

arr[x] = *it // where int x is part of a nested loop. I dont understand why this didn't work since I CAN do:
Check again if x is an integer and arr is not declared to be a constant this should work.

Originally Posted by JRM View Post
cout << *it // this will provide the dereferenced pointer data
What's the story here?
Think this should be clear now that you know the type of an iterator.
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: vector iterators vs operator []

 
0
  #4
Jun 18th, 2007
>1) interators are "smart access" in that they can keep track of the array position and size.
Huh? An index tells you the position, and an iterator doesn't tell you the size of the vector. You're still a little shaky on what iterators are, which is an abstraction (similar to a pointer) that lets you access a container without having to think about the internal structure of the containter. For example, you can start at begin() and go to end() in both a vector and a map and not care that one is probably an array and the other is probably a binary search tree. The iterator just works.

>2) the access element operator [] is the "dumb" access to the vector.
There's really no "smart" and "dumb" access. Iterators are sequential access: You start at x iterator and move to y iterator by means of a movement from x. Subscripting is random access: You pick an index and go right to it without needing an iterator to start from.

>arr=vint // why won't this work? essentially they're both arrays- or so i thought.
In the abstract sense they're both arrays. A vector is guaranteed to have consecutive elements, so it's almost always written as a dynamic array. However, a static array and a dynamic array are not the same thing. Even if they were, C++ doesn't support array assignment. There's no way that statement could do what you want.

>arr[it] = *it; // OK, I guess the iterator is a different animal than an integer?
You'll be better off thinking of iterators as potentially restricted pointers.

>arr[x] = *it // where int x is part of a nested loop.
Part of a nested loop? That doesn't sound right, but since you didn't post the code in question, it's impossible to tell you what's wrong. Provided all of the types are correct, memory is available, and the values are accurate, that statement should work correctly.

>I sure this all seems twisted to folks who are comfortable with the nuances
>I learn best by tinkering...
How do you think we became comfortable with the nuances?
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: vector iterators vs operator []

 
0
  #5
Jun 20th, 2007
Originally Posted by Narue View Post
>arr[it] = *it; // OK, I guess the iterator is a different animal than an integer?
You'll be better off thinking of iterators as potentially restricted pointers.
Wondering what you mean by the bolded part ? All my life I've worked with iterators by looking at them as pointer to contained type (as said in my post above).
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: vector iterators vs operator []

 
0
  #6
Jun 20th, 2007
>Wondering what you mean by the bolded part ?
There are different types of iterator policies. Only the random access iterator matches a pointer in functionality.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4291 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC