I have two classes which is a direct inheritance with no override, so they are basically: vector<string> list
and vector< reference_wrapper<string> > filtered
. The idea is I want to store all values into list
and then fill the filtered
with selected data from list
.
Now, when I do filtered.push_back()
if it has size of 1, the reference at index 0 would return an empty string (length = 0).
// concept code
int main() {
vector<string> list;
vector< reference_wrapper<string> > filtered;
string line;
for (;;) {
if (!getline(cin, line, '\n').good()) {
cout << "Bad input! Exiting..." << endl;
break;
} else if (line.length() > 0) {
break;
} else {
list.push_back(line);
// filtered[0].length() NOT 0 (size() = 1)
filtered.push_back(list.back());
// filtered[0].length() is now 0
}
// then print to screen... cout
}
Why does this happen?
Here's an example:
// cout when size() = 1
[1] Hello world
// cout when size() = 2
[1]
[2] Hi!
// cout when size() = 3
[1]
[2] Hi!
[3] My world