vector definition question from a beginner

I have a code which defines a variable in a function in the following way:

vector<vector<char> > const &data

Does that mean the &data constant is a vector of vectors, for example, something like: {{a,b,c},{d,e},{f,g,h}} ?

Thank you!
closed account (S6k9GNh0)
For starters, a more common way of declaring that line would be:
const std::vector< std::vector<char> > &data;

1) Using an ampersand before data tells the compiler to make a reference to a vector. You aren't actually making a vector. This is actually not a valid reference since all references must be assigned on initialization. I don't think this will compile.

2) I'm not sure if the compiler will try to make a vector name const which will not compile either. My way is correct. I think you can also do something like: std::vector< std::vector<char> > &data() const;
Last edited on
Thank you for your reply!

&data is a parameter of a function like this:
void function1(vector<vector<char> > const &data)

I guess if declare "using namespace std" then I do not need the std::vector?

But what does vector<vector<char> > mean? It is a vector? A vector of vectors?

Thank you!
It is a two dimensional array.
http://cplusplus.com/forum/articles/7459/
closed account (S6k9GNh0)
Take this for size:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>

using std::vector;

void vectExample(vector< vector<char> > &data)
{
    data.clear(); 
}

int main()
{
    vector< vector<char> > data(1);
    vectExample(data);
    return 0;
}


Even though data isn't an actual reference, this compiles and should work correctly. When using references in a function, it takes a reference to the variable passed automagically. This is to prevent heavy pass-by-value arguments.
Last edited on
Topic archived. No new replies allowed.