So I found this code on here that I'm working with:
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
struct Something{
int x;
int y;
};
int main()
{
vector <Something> v;
int x, y;
cin >> x >> y;
Something temp;
temp.x = x;
temp.y = y;
v.push_back(temp);
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << endl; // Error: No operator "<<" matches these operands. Operand types are std::ostream << Something
}
}
Basically, I'm trying to get multiple ints into one vector index.
cout
is not working when I try to print the contents of the vector.
First of all, am I even doing this multiple int thing right? I'm relatively new to C++.
And if I am doing this right, any ideas as to why cout
isn't working?
I also tried v.push_back({x,y})
and that didn't work. Any idea what is going on with cout
? Thanks a lot.
EDIT: Thank you very much so far. I just have one more question. If I were to modify my code to take multiple inputs and later wanted everything in the vector sorted according to "y" from largest to smallest.
Example (Original Vector Contents (x,y))
12 4
1 2
4 10
1 1
1 2
Sorted according to 'y' (largest to smallest)
4 10
12 4
1 2
1 2
1 1
I know how to do a regular sort but not one according to the second number (y). How do I do that? Thanks a lot.