vector help

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<vector<int> >row;
    vector<int> newColumn;

    row.push_back(newColumn);
    row.push_back(newColumn);
    row.push_back(newColumn);
    row.push_back(newColumn);

    row.at(0).push_back(1);
    row.at(1).push_back(2);
    row.at(2).push_back(3);
    row.at(3).push_back(4);

    cout << row[0][0] << "\t";
    cout << row[1][0] << "\t";
    cout << row[2][0] << "\t";
    cout << row[3][0] << endl << endl;

    row.at(0).pop_back();
    //row.at[0].back().pop_back();
    //I want to delete the element in [0][0]
    cout << row[0][0] <<  endl;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// http://ideone.com/V0icCs
#include <iostream>
#include <vector>

std::ostream& operator<<(std::ostream& os, const std::vector<int>& v)
{
    os << '{';

    if (v.empty())
        os << " empty";
    else
    {
        for (auto e : v)
            os << ' ' << e;
    }

    return os << " }";
}

std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<int>>& v)
{
    for (auto e : v)
        os << e << '\n';

    return os;
}

int main()
{
    std::vector<std::vector<int>> row{ { 1 }, { 2 }, { 3 }, { 4 } };

    std::cout << row << '\n';

    row[0].erase(row[0].begin());

    std::cout << row << '\n';
}
Last edited on
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.


Last edited on
Can anyone else help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<vector<int> >row;
    vector<int> newColumn;

    row.push_back(newColumn);
    row.push_back(newColumn);
    row.push_back(newColumn);
    row.push_back(newColumn);

    row.at(0).push_back(1);
    row.at(1).push_back(2);
    row.at(2).push_back(3);
    row.at(3).push_back(4);

    cout << row[0][0] << "\t";
    cout << row[1][0] << "\t";
    cout << row[2][0] << "\t";
    cout << row[3][0] << endl << endl;

    row.erase(row.begin());  /// do it and see the result
    //row.at[0].back().pop_back();
    //I want to delete the element in [0][0]
    cout << row[0][0] <<  endl;

}

@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.
Last edited on
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.
Last edited on
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.

pop_back works for me though...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "std_lib_facilities.h"

int main()
{
	vector<vector<int>> row{ { 5, 7, 8, 9 }, { 2, 3, 4, 6 }, { 5, 7, 3 } };
	for (auto v : row) {
		for (auto x : v) {
			cout << x << " ";
		}
		
		cout << endl;
	}

	row[2].pop_back();

	cout << endl;
	for (auto v : row) {
		for (auto x : v) {
			cout << x << " ";
		}
		
		cout << endl;
	}
	
	row[2].erase(row[2].end() - 1);

	cout << endl;
	for (auto v : row) {
		for (auto x : v) {
			cout << x << " ";
		}
		
		cout << endl;
	}
}


output:

5 7 8 9
2 3 4 6
5 7 3

5 7 8 9
2 3 4 6
5 7

5 7 8 9
2 3 4 6
5
Great it all works.

Thanks tipaye.

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.
Last edited on
"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];
    ...
}


You can find short but clear explanations of both auto and ranged for, as well as some other new features at: http://herbsutter.com/elements-of-modern-c-style/
Topic archived. No new replies allowed.