[英]Why it says 'push_back' has not been declared?
Why it says 'push_back' has not been declared ? 为什么说'push_back'还没有被宣布?
#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
; v[0]
是对vector
初始元素的引用; it isn't the vector
itself. 它不是vector
本身。 The element is of type int
, which is not a class type object and therefore has no member functions. 该元素的类型为int
,它不是类类型对象,因此没有成员函数。
Are you looking for v.push_back(0);
你在找v.push_back(0);
? ?
Note that vector<int> v(30);
注意vector<int> v(30);
creates the vector
with 30 elements in it, each with a value of zero. 创建包含30个元素的vector
,每个元素的值为零。 Calling v.push_back(0);
调用v.push_back(0);
will increase the size of the vector
to 31. This may or may not be the behavior your want; 将vector
的大小增加到31.这可能是也可能不是你想要的行为; if it isn't, you'll need to clarify what, exactly, you are trying to do. 如果不是,你需要澄清你正在尝试做什么。
你需要做v.push_back(0)
因为push_back
是向量的方法而不是它的元素。
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. 问题是v [0]是vector中的第一个元素,它是一个int。 The name of the vector is v. 向量的名称是v。
Just use v.push_back(0);
只需使用v.push_back(0);
You have to push_back into a vector. 你必须将push_back转换为向量。 Not into a specific element of a vector. 不是矢量的特定元素。
使用v.push_back(0)
因为v[0]
是一个int
而不是一个vector
。
You have the wrong type. 你的类型错了。
v
is of type Vector . v
是Vector类型。 v[0]
is NOT a vector, rather, it is a reference to the first element (which will be an int
). v[0]
不是向量,而是对第一个元素(将是int
)的引用。
As a result, v[0]
does not have a push_back
method. 因此, v[0]
没有push_back
方法。
Only the vector itself ( v
) has the method. 只有矢量本身( v
)才有方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.