C++ 访问c+中向量内部的数组+;

C++ 访问c+中向量内部的数组+;,c++,arrays,vector,C++,Arrays,Vector,我有这段代码 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]

我有这段代码

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]);
    }
}
std::vector向量;
国际原子能机构[2];
无符号anArraySize=sizeof(anArray)/sizeof(int);
对于(无符号整数j=0;j<100;j+=10){
for(无符号整数i=0;i<100;i+=3){
anArray[0]=j;
anArray[1]=i;
插入(aVector.end(),&anArray[0],&anArray[anArraySize]);
}
}
它基本上是将一个大小为2(0,1)的数组插入到名为ijVector的向量中

现在,我想为aVector中的每个值访问anArray[0]和anArray[1]中的anArray值

比如说

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
}
for(int i=0;i-->aVector.size()-1;){

std::cout每个偶数索引都是一个数组的第一个元素,每个奇数索引都是该数组的第二个元素。数组中的元素数是假设的两倍

此代码应适用于:

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;
}
<代码>for(int i=0;istd::cout每个偶数索引都是一个数组的第一个元素,每个奇数索引都是该数组的第二个元素。数组中的元素数是假设的两倍

此代码应适用于:

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;
}
for(int i=0;i
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}});
    }
}
std::vector向量;
for(无符号整数i=0;i<100;i+=10){
对于(无符号整数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;
}
for(unsigned int i=0;i!=aVector.size();++i){
std::cout您可能需要

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}});
    }
}
std::vector向量;
for(无符号整数i=0;i<100;i+=10){
对于(无符号整数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;
}
for(unsigned int i=0;i!=aVector.size();++i){

std::cout声明为
std::vector 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;
}
我建议改用

const unsigned int anArraySize = 2;
int anArray[anArraySize ];

声明为
std::vector 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;
}
我建议改用

const unsigned int anArraySize = 2;
int anArray[anArraySize ];

为什么不使用
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;
int main()
{
typedef std::pair intPair;
typedef std::vector intPairVec;
intPairVec aVector;
对于(无符号整数j=0;j<100;j+=10){
for(无符号整数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为什么不使用
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;
int main()
{
typedef std::pair intPair;
typedef std::vector intPairVec;
intPairVec aVector;
对于(无符号整数j=0;j<100;j+=10){
for(无符号整数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问题在于您的向量是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);
    }
}

问题是您的向量是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);
    }
}

向量不包含数组,它包含从数组中复制的单个整数。您需要改变思考问题的方式。P.S.
-->
是合法语法,但不执行您认为它可以执行的操作。向量不包含数组,它包含从数组中复制的单个整数。您可以ed想改变你思考问题的方式。P.S.
-->
是法律语法,但不符合你的想法。其他解决方案非常有用,但我完全忘记了“一对”。非常感谢你提醒我!其他解决方案非常有用,但我完全忘记了“一对”。非常感谢你提醒我!