I really don't understand what happen. In my test program, it works :
1 2 3 4 5 6 7 8 9 10 11 12
vector<string> t1;
cout << "t1 should be empty - size = " << t1.size() << ", contents : "; for (u_int32_t i = 0; i < t1.size(); i++) cout << t1[i] << " "; cout << endl;
t1.push_back("str1");
cout << "t1 should have 1 element - size = " << t1.size() << ", contents : "; for (u_int32_t i = 0; i < t1.size(); i++) cout << t1[i] << " "; cout << endl;
t1.push_back("str2");
cout << "t1 should have 2 elements - size = " << t1.size() << ", contents : "; for (u_int32_t i = 0; i < t1.size(); i++) cout << t1[i] << " "; cout << endl;
vector<string> t2 = {"str1", "str2"};
cout << "t2 should have 2 elements - size = " << t2.size() << ", contents : "; for (u_int32_t i = 0; i < t2.size(); i++) cout << t2[i] << " "; cout << endl;
t2.push_back("str3");
cout << "t2 should have 3 elements - size = " << t2.size() << ", contents : "; for (u_int32_t i = 0; i < t2.size(); i++) cout << t2[i] << " "; cout << endl;
t2.push_back("str4");
cout << "t2 should have 4 elements - size = " << t2.size() << ", contents : "; for (u_int32_t i = 0; i < t2.size(); i++) cout << t2[i] << " "; cout << endl;
t1 should be empty - size = 0, contents :
t1 should have 1 element - size = 1, contents : str1
t1 should have 2 elements - size = 2, contents : str1 str2
t2 should have 2 elements - size = 2, contents : str1 str2
t2 should have 3 elements - size = 3, contents : str1 str2 str3
t2 should have 4 elements - size = 4, contents : str1 str2 str3 str4
But in my application, push_back does nothing. I checked with the debugger, it does nothing, nor throw anything, nor complain.
1 2 3
vector<string> col = {"col1", "col2"};
// some code
col.push_back("col3");
col before push_back = "col1", "col2"
col after push_back = "col1", "col2"
hmmm! can u expect people to go through it, no, but u r lucky, i like vectors, please paste ur code in proper format next time.
To traverse a vector use a vector iterator:
There is nothing wrong with using an index to traverse a vector. He doesn't need to modify his code at all.
EDIT:
That said, for other container types like a deque, list, etc.... yes you should use iterators. And it makes sense to use iterators with vectors also for sake of consistency.
But it's not worth going back and changing the code for.
t exists before the constructor is called !!! t is created in other and previous sections of the code, but in separate blocks that should have led it to be destroyed. I don't thing the compiler or the debugger anticipates ?
Local variables are all pushed to the stack at the very start of the function, regardless of where in the function they are declared. So even if you declare a variable at the very last line of a function, the debugger will see it at the very start of the function.
However, the constructor is not called until the line in which you declare the variable is reached... so if you look at 't' in the debugger before it's declared, you'll notice it contains garbage / uninitialized values.
Yes, that was the question. Thanks Disch. But the data in t before the constructor is called TableInFile t(file1_name, col, typ, vec, mod); is not garbage !!!