2

I have the following code fragment

#include <iostream>
#include <ostream>
#include <vector>
using namespace std;

int main() {
    vector<vector<int>>v;
    return 0;  
}

v.push_back(11) does not work what is correct?

David Nehme
  • 21,379
  • 8
  • 78
  • 117

2 Answers2

6
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;

int main() {
    vector<vector<int> >v;
    vector<int> a;
    a.push_back(11);
    v.push_back(a);
    return 0;  
}

I think this should work right :)

Arjit
  • 565
  • 9
  • 27
2
vector<vector<int>>v;

needs to be

vector<vector<int> >v;

The consecutive >> acts as the actual >> operator.

advait
  • 6,355
  • 4
  • 29
  • 39
  • there should also be a space before `v`. – Amir Rachum Aug 09 '10 at 08:29
  • 2
    In Visual C++ 2008 Sp1 `vector> v;` is fine. – graham.reeds Aug 09 '10 at 08:33
  • ...and most modern compilers. – Blindy Aug 09 '10 at 08:41
  • @Blindy: If modern compilers are not interpreting >> as the operator >> then they are incorrect (So I doubt that statement is correct). It is only valid to interpret them as closing template braces when compiling C++0x where the language definition has been changed to allow for this. If different compilers interpreted the same source in different ways we would be back in the 60's (standards conformance is everything nowadays). – Martin York Aug 09 '10 at 14:00
  • @Martin York, parsing >> as the closing part of a template is actually unambigous. There is no possible way of using the >> operator WITHIN a template type and make it a legal statement. Also pick up a copy of VS 2010 and try it for yourself. – Blindy Aug 09 '10 at 20:05
  • And it's not about interpreting it "differently", it's either interpreting it the right way (C++0x) or erroring out (your 60's compilers, mind you we live in 2010). – Blindy Aug 09 '10 at 20:06
  • 1
    @Blindy: Big claim for `unambigous` :-). I bet this `T1> b > > d;` is parsed differently in C++03 and C+0x. I would expect this to be a variable declaration for 'd' but if you parse '>>' as two seprate tokens (as you could in C++0x) then it is a syntax error. – Martin York Aug 09 '10 at 22:08
  • I see what you're saying and that can be (and is) fixed by adding parantheses to your expression: `T1> b) > > d;`. This works in both current C++ and C++0x. – Blindy Aug 09 '10 at 23:56