So I'm trying to delete a value stored inside one of my vectors but I can't accomplish this. My attempts are down below. I've commented out one attempt since it gives me errors. How can I do what I am trying to do?
When I try to run the code provided by cire I get the error that e does not name a type.
I'm really not familiar with the ostream& operator. I know your referencing the ostream class to overload the << operator if I'm correct but I don't know else goes on after that.
Help me understand this as well someone.
1 2 3 4 5 6 7 8 9 10 11
os << '{';
if (v.empty())
os << " empty";
else
{
for (auto e : v)
os << ' ' << e;
}
return os << " }";
I get that os << '{'; prints this to the console window but what does os mean exactly? Is it like saying out?
And I suspect that for(auto e : v) is an iterator that goes through the range e through v?
I still can't get my code to do what I want it to.
@sujitnag, that just erases the entire vector row[0], I think what he's after is " row[0].erase(row[0].begin());" as per Cire.
cppnoob, what's your compile command line? Cire's code should compile and run. Maybe you're not using the right flags to force C++11 compatibility. Most compilers still default to c++98
g++ cire.cpp won't work, but g++ -std=c++11 cire.cpp will, if you're using a recent version of gcc
Otherwise confirm that your compiler supports c++11, and check what the necessary flag is.
Cire has used a new form of initialisation and a new form of for loop (ranged for), that were unavailable in C++98. As you can see, it makes code cleaner and life a lot easier once you get used to it.
You're rigjt @tipaye I'm just trying to delete the first item in row[0]. There is more code to this that further fills those rows and I don't want to delete those other items.
But I am using the code blocks IDE which is using MingW. I'll look into getting it to support c++11 and then try cires code again.
Cires code works great but I guess I should have been more clear on what I'm trying to accomplish.
I was to push numbers into rows and columns through a vector but I want to be able to do this.
Say I have this.
5 7 8 9
2 3 4 6
5 7 3
I want to be able to remove the number 3. With Cires code I can only remove the 8 as it removes the number at the begining. I couldn't figure out how to make it so that it works on the last element added to that vector. pop_back doesn't seem to work here. How can I remove [2][2] without hard coding that in case I push more numbers in there? I want to be able to remove the last number added into the vector.
Instead of row[0].erase(row[0].begin()); which will remove the first element
Try row[0].erase(row[0].end() - 1); which I hope will remove the last element.
Just one more thing. I've never use auto before and I would like to know how it works.
1 2 3 4 5 6 7
for (auto v : row) {
for (auto x : v) {
cout << x << " ";
}
cout << endl;
}
what exactly is going on there? I get that it's printing out the numbers from row to row but aside from that I can't understand the arguments there. I can make guesses but I'd like to know what auto and the colon do.
I did look this up but I only ran into more complicated examples.
"auto" automatically figures out the type, very useful for complex situations where the type is not so obvious. In this case, we could have written it as for
1 2 3 4 5
for (vector<int> v : row) {
for (int x : v) {
cout << x << " ";
}
}
the ranged for may be considered in the following light:
1 2 3 4
for (int x : v)
{
...
}
has the same effect logically, as
1 2 3 4 5
for (int i = 0; i < v.size(); ++i)
{
int x = v[i];
...
}