Hi all,
I would like to initialise a vector of vectors with existing vectors, like this :
vector<vector<uchar> > vectorOfVector = {mVec1, mEVec2, mVec3};
Here, I am in a method definition of a class where these vectors are declared as attributes :
If I proceed like that, it compiles, executes, but when I deal with vectorOfVector[0].push_back('a');
for example, mVec1 does not receive 'a' - it remains empty.
If I check the vector address, &mVec1 != &vectorOfVector[0], so the method to initialise vectorOfVector I used is not correct.
If I try with references, like that : vector<vector<uchar>& > vectorOfVector = {mVec1, mEVec2, mVec3};
The compiler issue an error :
‘const’ qualifiers cannot be applied to ‘std::vector<unsigned char>&’
Cuuldn't you make a vector of a structure of 3 chars (or an array of 3 chars), and just keep a reference to that vector?
For the compilation errror, you might be passing mVec1, mVec2 and MVec3 as const arguments to the current function, and since you keep a reference and not a copy, the constness would have to be kept
You could create a class "myVector" then use those 3 vectors as your public variable then create another one as a private to create an overloaded operator that can access and store the data.
The answer is no. For the internal vector<uchar>, I don't know a priori their size. They are filled with push_back and may have a few elements or hundreds. So I need the flexibility of vectors, and so the use of vector<vector<uchar> >.
One alternative that should work is vector<vector<uchar>* >, but I would like to avoid it because it is far heavy to manage.
When I try to analyse what happens here : vector<vector<uchar> > vectorOfVector = {mVec1, mEVec2, mVec3};
I think it uses the default constructor and then pass the values. But it does not delete the objects created to use the existing ones instead. I don't understand why the compiler does not issue an error here.
On the contrary, I don't understand why the reference version : vector<vector<uchar>& > vectorOfVector = {mVec1, mEVec2, mVec3};
does not work ???
Error : could not convert ‘{((LineObject*)this)->LineObject::mVec1, ((LineObject*)this)->LineObject::mVec2, ((LineObject*)this)->LineObject::mVec3}’ to ‘std::vector<std::reference_wrapper<std::vector<unsigned char> > >’
I think the error does not deal with your declaration of a vector of vectors. For example the following code is successfully compiled and executed with GCC 4.7.1
@vlad from moscow : waaooo !!!! But I have only gcc 4.5.3 available. So no auto pointers. The good news is, with a little adaptation to compensate for the lack of auto pointers, it works :
class C64 {
public:
vector<char> v1;
vector<char> v2;
void printV() {
vector<vector<char> > v = { v1, v2 };
for (char i = 'a'; i < 'd'; i++) v1.push_back(i);
for (char i = '1'; i < '4'; i++) v2.push_back(i);
for ( size_t i = 0; i < v.size(); i++ ) {
for ( size_t j = 0; j < v[i].size(); j++) cout << v[i][j];
std::cout << std::endl;
}
}
};
int main() {
C64 t;
t.printV();
}
I have only the endl in the output !
@Cubbi : your link looks dead. Thanks for the update. It does not compile for me. What gcc version do you use ? I have added #include <utility> but it is the same.
@Cubbi : Thanks for the update. It does not compile for me. What gcc version do you use ?
The one used at ideone.com is 4.5.1.
Reference wrappers have been around for a long time, even gcc 4.1.2 (the oldest gcc I can access) has them, in tr1 namespace. But since you're using list-initializer syntax for your vectors in the first post, i assumed your compiler was at least gcc-4.5 or something equally recent.
I could install gcc-4.7.1 for test. But still, in C64, I cannot write v[i][j] instead of v[i].get()[j]
Compilator says :
error : no match for ‘operator[]’ in ‘v.std::vector<_Tp, _Alloc>::operator[]<std::reference_wrapper<std::vector<char> >, std::allocator<std::reference_wrapper<std::vector<char> > > >(i)[j]’
But probably I can now benefit from move features.....