开发者

Why it says 'push_back' has not been declared?

Why it says 'push_开发者_JAVA技巧back' has not been declared ?

#include <iostream>
#include <vector>
using namespace std;
int main()
{
  vector <int> v(30);
  v[0].push_back(0);
 return 0;
}


v[0] is a reference to the initial element in the vector; it isn't the vector itself. The element is of type int, which is not a class type object and therefore has no member functions.

Are you looking for v.push_back(0);?

Note that vector<int> v(30); creates the vector with 30 elements in it, each with a value of zero. Calling v.push_back(0); will increase the size of the vector to 31. This may or may not be the behavior your want; if it isn't, you'll need to clarify what, exactly, you are trying to do.


You need to do v.push_back(0) as push_back is the method of the vector not its element.


Try this:

#include <iostream>
#include <vector>
using namespace std;
int main()
{
  vector <int> v(30);
  v.push_back(0);
  return 0;
}

The problem is that v[0] is the first element in vector, which is an int. The name of the vector is v.


Just use v.push_back(0); You have to push_back into a vector. Not into a specific element of a vector.


You have the wrong type.

v is of type Vector. v[0] is NOT a vector, rather, it is a reference to the first element (which will be an int).

As a result, v[0] does not have a push_back method.
Only the vector itself (v) has the method.


use v.push_back(0) as v[0] is an int and not a vector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜