1

I have this piece of code.

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

It is basically inserting an array of size two(0, 1) into the vector called ijVector.

Now, I would like to access the anArray values in anArray[0] and anArray[1] for each value in aVector.

For example, something like

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
}

How do I get the value inside array of each vector?

js0823
  • 1,843
  • 8
  • 24
  • 36
  • 1
    The vector does not contain the arrays, it contains individual ints which were copied from the arrays. You need to change the way you're thinking of the problem. P.S. `-->` is legal syntax but doesn't do what you think it does. – Mark Ransom Oct 01 '15 at 19:40

5 Answers5

3

Why don't you use std::pair?

You can do it like this:

#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;
}
SHR
  • 7,940
  • 9
  • 38
  • 57
  • Other solutions were very helpful but the "pair" I totally forgot about. Thank you so much for reminding me! – js0823 Oct 01 '15 at 20:32
2

You may want

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

And later

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;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
2

The problem is that your vector is a vector of int, not a vector of arrays of length 2. Your array could reasonably defined and populated as follows:

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

This way you can make calls of the form

aVector[x][y]
BMurray
  • 341
  • 2
  • 5
1

Every even index is the first element of one array, and every odd index is the second element to the array. There are twice as many elements in your array than you are assuming.

This code should work:

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;
}
owacoder
  • 4,815
  • 20
  • 47
1

The vector declared as std::vector<int> aVector; does not contains element that are array of 2 integers each, it instead contains elements that are integers. Inside the vector aVector you don't have smaller array of 2 elements but only the integers.

One possibility is adding the elements of your array one after each other

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

Please note you cannot declare with STL

As noted in Can i push an array of int to a C++ vector? and Correct way to work with vector of arrays .

Another note, this is a correct but curious way to get the size of the array.

int anArray[2];
unsigned anArraySize = sizeof(anArray) / sizeof(int);

I would suggest using instead

const unsigned int anArraySize = 2;
int anArray[anArraySize ];
Community
  • 1
  • 1
Lorenzo Belli
  • 1,767
  • 4
  • 25
  • 46