having trouble understanding this statement

This is a discussion on having trouble understanding this statement within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble understanding the following statement: Code: std::vector<int>::iterator location; What is throwing me off is the scope resolution operator ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    having trouble understanding this statement

    I'm having trouble understanding the following statement:
    Code:
    std::vector<int>::iterator location;
    What is throwing me off is the scope resolution operator :: and the iterator, or ::iterator.
    What does the ::iterator do?
    I know that the statement is intended to instantiate location as an integer vector. How is this statement stated or pronouned and, again, what does the ::iterator mean?

  2. #2
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    The scope operator serve to access things which are nested into other declarations.
    Here, the namespace std exports the vector<int> class, in this class lies another one which is the iterator, that way, you have to use the operator in order to have access to it.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Originally posted by kes103
    I know that the statement is intended to instantiate location as an integer vector.
    Actually, that is not true. The statement is intended to instantiate location as an iterator that can be used with a vector of integers.

    The iterator class, like lyx said, is nested within the vector class. For all of the STL containers there is an iterator and a const_iterator. These objects are used to identify a single position within the container.

    For example, if you wanted to go through each element in your vector (in other words, iterate over it), you would declare an iterator, then set it to the beginning of the vector and increment it one at a time until you reach the end. Here is an example that adds two to each entry in the vector:
    Code:
    std::vector<int> intVec;  // Instantiate intVec as an integer vector
    std::vector<int>::iterator location;  // Create iterator for integer vector.
    ...
    // Get an iterator that points to one past the last element.
    std::vector<int>::iterator end = intVec.end();
    
    // Make the iterator point to the first element.
    location = intVec.begin();
    
    // Keep incrementing until the current location reaches the end;
    for (; location != end(); ++location)
    {
        (*location) += 2;
    }
    As far saying it, you rarely read it aloud. I guess you would just call location an iterator and it if necessary make the distinction that it is an iterator for a vector of ints.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If statement being ignored?
    By FincH in forum C Programming
    Replies: 3
    Last Post: 04-18-2007, 02:51 PM
  2. if statement Help!
    By C++Noobie in forum C++ Programming
    Replies: 12
    Last Post: 11-10-2006, 11:44 AM
  3. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 03:57 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 10:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21