在c++中访问vector中的数组
Accessing array inside vector in c++
我有这段代码。
std::vector<int> aVector;
int anArray[2];
unsigned anArraySize = sizeof(anArray) / sizeof(int);
for (unsigned int j = 0; j < 100; j += 10) {
for (unsigned int i = 0; i < 100; i += 3) {
anArray[0] = j;
anArray[1] = i;
aVector.insert(aVector.end(), &anArray[0], &anArray[anArraySize]);
}
}
它基本上是将一个大小为2(0,1)的数组插入到名为ijVector的向量中。
现在,我想访问anArray[0]和anArray[1]中的anArray值对于vector中的每个值。
例如,像
for (int i = 0; i --> aVector.size() - 1;) {
std::cout << "aVector[" << i << "].anArray[0] = " << aVector.anArray[0] << std::endl; // getting value is wrong
std::cout << "aVector[" << i << "].anArray[1] = " << aVector.anArray[1] << std::endl; // getting value is wrong
}
我如何得到每个向量数组内的值?
为什么不用std::pair
呢?
你可以这样做:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
typedef std::pair<int,int> intPair;
typedef std::vector<intPair> intPairVec;
intPairVec aVector;
for (unsigned int j = 0; j < 100; j += 10) {
for (unsigned int i = 0; i < 100; i += 3) {
aVector.push_back(std::make_pair(j,i));
}
}
int i=0;
for (intPairVec::iterator it = aVector.begin(); it != aVector.end();it++) {
std::cout << "aVector[" << i << "].1st = " << it->first << std::endl;
std::cout << "aVector[" << i << "].2nd = " << it->second<< std::endl;
i++;
}
return 0;
}
您可能需要
std::vector<std::array<int, 2>> aVector;
for (unsigned int i = 0; i < 100; i += 10) {
for (unsigned int j = 0; j < 100; j += 3) {
aVector.push_back({{i, j}});
}
}
和
for (unsigned int i = 0; i != aVector.size(); ++i) {
std::cout << "aVector[" << i << "].anArray[0] = " << aVector[i][0] << std::endl;
std::cout << "aVector[" << i << "].anArray[1] = " << aVector[i][1] << std::endl;
}
问题是您的向量是int型向量,而不是长度为2的数组向量。您的数组可以合理地定义和填充如下:
std::vector<std::array<int, 2> > aVector;
for (unsigned int j = 0; j < 100; j += 10) {
for (unsigned int i = 0; i < 100; i += 3) {
std::array<int, 2> a = {j, i};
aVector.push_back(a);
}
}
这样你就可以调用
aVector[x][y]
声明为std::vector<int> aVector;
的向量不包含每个元素为2个整数的数组,而是包含整数元素。在向量aVector
中,你没有2个元素的较小数组,而只有整数。
一种可能是将数组中的元素一个接一个地添加
for (int i = 0; i < aVector.size()/2; ++i)
{
std::cout << "aVector[" << i << "].anArray[0] = " << aVector.at(2*i) << std::endl;
std::cout << "aVector[" << i << "].anArray[1] = " << aVector.at(2*i+1) << std::endl;
}
请注意不能用STL
声明如前所述,我可以将int数组推入c++向量吗?
另一个注意事项,这是一个正确但奇怪的方法来获取数组的大小。
int anArray[2];
unsigned anArraySize = sizeof(anArray) / sizeof(int);
我建议使用
const unsigned int anArraySize = 2;
int anArray[anArraySize ];
每个偶数索引是一个数组的第一个元素,每个奇数索引是该数组的第二个元素。
下面的代码应该可以工作了:
for (int i = 0; i < aVector.size(); ++i) {
std::cout << "aVector[" << i << "].anArray[0] = " << aVector.at(i/2) << std::endl;
std::cout << "aVector[" << i << "].anArray[1] = " << aVector.at(i/2+1) << std::endl;
}
相关文章:
- 在c++中用vector填充一个简单的动态数组
- std::vector的包装器,使数组的结构看起来像结构的数组
- 将 int 数组转换为 std::vector<int*>
- 为什么std::vector比数组慢
- C++ 无法将 std::vector<BYTE> 转换为字符串或字符数组
- 如何将数组的内容附加到 std::vector?
- 在 C++ 中使用 Vector 在动态数组中插入值,循环
- 为什么优化大型 std::vector 数组会导致 SegFault?
- SFML访问std::vector内的数组
- 如何在C++中将 std::vector 移动到原始数组中
- 如何使 std::vector<T> 使用现有的 T* 数组而不复制这些值?
- 将 unique_ptr<std::vector> 与 c 样式数组结合使用
- 将 2D 数组插入 std::vector 时"cannot convert from 'const GLfloat [12]' to '_Objty'"错误消息
- 查找索引数据结构,例如`sTD :: vector`(非数组)
- 如何在多个文件中声明全局 std::vector 2d 数组?C++
- 删除[]是否可以与通用数组正常使用?如果是这样,为什么使用std :: vector ::擦除它会导致释放内存的错误
- 使用 ::std::vector 创建数组
- 为什么"new vector<int>[5]"不创建向量数组?
- 如何将std ::数组转换为std :: vector
- Bool Vector 2D数组在IF条件下使用