Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 range-based for on a vector of pointers

Tags:

c++

pointers

gcc

I have just compiled GCC 4.6.0, and I wanted to try the new features out, starting with the range-based for loop.
The first loop I wanted to change was iterating on a std::vector of pointers. I changed the code to use the new syntax, but it didn't compile.

I have tried to substitute another for loop, which was on a std::vector of structs, and it compiled and ran perfectly.

Here is a short test code to show you my problem:

#include <vector>
#include <iostream>

int main()
{
    std::vector< int > values;
    
    values.push_back(2);
    values.push_back(5);
    values.push_back(8);
    values.push_back(13);
    values.push_back(17);
    
    for (int &n : values)
    {
        std::cout << n << "\n";
    }
    
    std::vector< int* > pointers;
    
    pointers.push_back(new int(2));
    pointers.push_back(new int(5));
    pointers.push_back(new int(8));
    pointers.push_back(new int(13));
    pointers.push_back(new int(17));
    
    for ((int*) &p : values)
    {
        std::cout << (*p) << "\n";
    }
    
    for( unsigned int i = 0; i < pointers.size(); ++i)
    {
        delete pointers[i];
    }
    
    return 0;
}

When I try to compile it (yes, I give -std=c++0x as a parameter to g++), it dies with this error:

main.cpp|27|error: found ‘:’ in nested-name-specifier, expected ‘::’

If I comment the lines 27-30 out, it's OK.

What am I doing wrong? Isn't the pointer-reference declaring syntax right?
Or is there a limitation of contained types where range-based for loops can be used?

like image 852
Attila Avatar asked Apr 18 '11 19:04

Attila


People also ask

Can you use pointers with vectors?

You can store pointers in a vector just like you would anything else. Declare a vector of pointers like this: vector<MyClass*> vec; The important thing to remember is that a vector stores values without regard for what those values represent.

What is a vector range?

If X is a vector, then range(X) is the range of the values in X . If X is a matrix, then range(X) is a row vector containing the range of each column in X . If X is a multidimensional array, then range operates along the first nonsingleton dimension of X , treating the values as vectors.

Are C++ vectors pointers?

An ordinary vector encountered in C++ programming, is a vector of objects of the same type. These objects can be fundamental objects or objects instantiated from a class. This article illustrates examples of vector of pointers, to same object type.

Is STD Vector a pointer?

std::vector is a sequence container that encapsulates dynamic size arrays. So definately, it is not a pointer.


2 Answers

for ((int*) &p : values)

This is wrong. (int*) is an expression alone, so you need to do int*& (with no parenthesis, that makes an expression - aka "not a type name") at least to make it correct. I prefer to use auto or auto&, personally.

You can do :

for (auto p : values) // here p is a pointer, a copy of each pointer

or

for (auto& p : values ) // here p is a non-const reference to a pointer

or

for ( int* p : values ) // here p is a copy of each pointer

or in generic code:

for ( auto&& p: values ) // p is either a const reference to what is in values, or a non-const reference, depends on the context
like image 159
Klaim Avatar answered Sep 18 '22 19:09

Klaim


I think you meant to iterate over 'pointers' instead of 'values' there...

like image 30
Frits Avatar answered Sep 19 '22 19:09

Frits