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:

#include <iostream>
#include <vector>
using namespace std;

vector<int> vint;
int input =1 ;

int main()
{
        cout << "Enter an integer (Enter 0 to exit) ";


        while(input)
        {
        cin >> input;
        vint.push_back(input);
        }

        size_t n = vint.size();
        int arr[n];
                cout << "the vector content is: ";
                for ( int j  = 0; j < n; j++)
                {
                arr[j] = vint[j];
                cout << vint[j] << " ";
                }
        cout << endl;
        cout << "The vector size is: " << vint.size() << endl;
        cout << "The content of the array is: "<< endl;
                for (int j=0; j < n; j++)
                        cout << arr[j] << " ";
                cout << endl;


        return 0;
}

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...

#include <vector>
#include <algorithm>
using namespace std ;
int main()
{
  vector<int> cntr( 20U, 7 ) ;
  int* array = new int [ cntr.size() ] ;
  typedef vector<int>::iterator iterator ;
  int x = 0 ;
  for( iterator it = cntr.begin() ; it != cntr.end() ; ++it, ++x )
  {
   array[x] = *it ; // ok
   // array[it] = *it ; // error, it is not an integral value
  }
  
  // the easy way
  copy( cntr.begin(), cntr.end(), array ) ;
  delete[] array ;
}

>> size_t n = vint.size();
>> int arr[n];
This is not supposed to work !

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).

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.

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.

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.

>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? :icon_rolleyes:

>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).

>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.

This article has been dead for over six months. Start a new discussion instead.