在c++中访问vector中的数组

Accessing array inside vector in c++

本文关键字:数组 vector 访问 c++      更新时间:2023-10-16

我有这段代码。

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;
}