Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 派生向量:执行操作_C++_Vector - Fatal编程技术网

C++ 派生向量:执行操作

C++ 派生向量:执行操作,c++,vector,C++,Vector,我从std::vector(我知道我不应该,但我只是想测试它)。现在我可以实例化它并分配一些值: MyVector v(5); v[0]=3; 我甚至可以返回值: cout << v[0]; 如问题下的评论所述: 返回(*本)[0]+a;应该有效5小时前的迪迪克 此外,由于vector以线性方式(如数组)布置内存,您还可以通过指针访问保存值的内存,如下所示: int *ptr = &(*this)[0]; // read an integer from the conso

我从
std::vector
(我知道我不应该,但我只是想测试它)。现在我可以实例化它并分配一些值:

MyVector v(5);
v[0]=3;
我甚至可以返回值:

cout << v[0];

如问题下的评论所述:

返回(*本)[0]+a;应该有效5小时前的迪迪克

此外,由于
vector
以线性方式(如数组)布置内存,您还可以通过指针访问保存值的内存,如下所示:

int *ptr = &(*this)[0];
// read an integer from the console into the 3rd element of the vector
scanf("%d", ptr + 2);
例如,如果您有一个字符的
向量
,并且需要将
字符*
传递给字符串函数之类的函数,那么这将非常有用


但是,请注意,
vector
的行为方式不同(布尔值在内部存储在位字段中,而不是布尔数组中,请参见)

返回(*this)[0]+a应该可以工作。
(*this)[0]
this->operator[](0)
this->at(0)
:注意最后一个做边界检查并抛出边界。@ThePhD:
operator[](0)
在没有
这个->
的情况下工作。非常正确,所以他有很多选择D
int *ptr = &(*this)[0];
// read an integer from the console into the 3rd element of the vector
scanf("%d", ptr + 2);