Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on a project and it would be much easier if I could fill an int array with more than one int value.

For example:

int array[5];

array[0] == 10, 20, 30;
array[1] == 44, 55, 66;
...

It's kinda hard for me to explain, but how could I fill an array with multiple int values? Thanks for your time :)

share|improve this question
4  
First of all == is equality not assignment(=), second can you provide a little more context. – Shafik Yaghmour Jul 13 at 2:05
It was simply my way of expressing what I was looking for. I have an array (slotId[40]) and for my project, each slot has 3 values. I'm wondering how to make on array hold 3 int's per sector. – mc360pro Jul 13 at 2:07

4 Answers

You can declare a multidimensional array:

int array[5][3];
array[0][0] = 10; array[0][1] = 20; array[0][2] = 30;
array[1][0] = 44; array[1][1] = 55; array[2][2] = 66;
...

or create an array of an ad-hoc struct:

struct tuple_3int
{
    int x, y, z;
    tuple_3int() {} 
    tuple_3int(int X, int Y, int Z) : x(X), y(Y), z(Z) {}
};

tuple_3int array[5];
array[0] = tuple_3int(10, 20, 30);
array[1] = tuple_3int(44, 55, 66);

Or, if you are using C++11, you can use the new tuples, and declare an array of tuples of 3 ints:

#include <tuple>

std::tuple<int, int, int> array[5];
array[0]=std::make_tuple(10, 20, 30);
array[1]=std::make_tuple(44, 55, 66);
share|improve this answer
That struct looks like what I need. Let me mess with it and I'll get back to you :) – mc360pro Jul 13 at 2:13
I'm getting a bunch of errors. I'm sure it's not your code, rather it's me not understanding how to implement this with my current program. The example I provided wasn't a real example. Let's just say I though it would be simpler :/ – mc360pro Jul 13 at 2:35
You might also want to explain the usage of std::get in the C++11 example. – user9000 Jul 13 at 2:35
@mc360pro The struct example looks like it requires a default constructor the way it is setup, I will add a similar example to my answer. – Shafik Yaghmour Jul 13 at 2:54
@mc360pro: Sorry, it needed a default constructor. Now it should be ok. – Matteo Italia Jul 13 at 3:33

You can achive your goal at diffrent ways:

  1. Create a triple class of int's, and create array of this class.

i.e.

class triple {
int x;
int y;
int z;
public:
triple (int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
};

triple array[SIZE]; array[0] = triple (1,2,3);

  1. Enter the values of the int ordinary and refer for every 3 sequential cells as 1 index.

i.e.

array[0] = 10;    
array[1] = 44;   
array[2] = 20;   
array[3] = 55;   
array[4] = 30;   
array[5] = 66;  

Than 0-2 indexes will be your first cell, 3-5 the seconde and so on.

  1. Creat a 2D array.

i.e.

int array [SIZE][3];
array [i][0] = 1;
array [i][1] = 2;
array [i][2] = 3;
share|improve this answer
I'm attempting your first suggestion as that is what I need. In my program, I send the array to a function where the values are changed (they're initialized outside the function). But I keep getting errors about syntax "triple" and now "slotId." – mc360pro Jul 13 at 2:32
I can't tell you what is it without to see the code. However notice that if you need access to the (x,y,z) fileds you need to: 1) make them public fileds 2) create a get and\or set methods 3) define the class as a struct. – Ran Eldan Jul 13 at 2:47
I suppose I'm too basic with C++, especially in this Qt gui I'm making. I'll mess around and get back to you. – mc360pro Jul 13 at 2:52

Assuming you can use C++11, then a std::vector of std::tuple seems to be a simple approach, here is a simple example:

#include <iostream>
#include <vector>
#include <tuple>

int main()
{
    std::vector<std::tuple<int,int,int>> tupleVector ;

    tupleVector.push_back( std::make_tuple( 10, 20, 30 ) ) ;
    tupleVector.push_back( std::make_tuple( 44, 55, 66 ) ) ;

    std::cout << std::get<0>( tupleVector[0] ) << ":" << std::get<1>( tupleVector[0] )  << ":" << std::get<2>( tupleVector[0] )  << std::endl ;
    std::cout << std::get<0>( tupleVector[1] ) << ":" << std::get<1>( tupleVector[1] )  << ":" << std::get<2>( tupleVector[1] )  << std::endl ;
}

A non C++11 example could use a struct to hold the slot data, you could stick with arrays but std::vector is simpler and will cause you less headaches in the long run:

#include <iostream>
#include <vector>
#include <tuple>

struct slot
{
    int x1, x2, x3 ;

    slot() : x1(0), x2(0), x3() {}   // Note, need default ctor for array example
    slot( int p1, int p2, int p3 ) : x1(p1), x2(p2), x3(p3) {}  
} ;

int main()
{
    std::vector<slot> slotVector ;

    slotVector.push_back( slot( 10, 20, 30 ) ) ;
    slotVector.push_back( slot( 44, 55, 66 ) ) ;

    std::cout << slotVector[0].x1 << ":" << slotVector[0].x2 << ":" << slotVector[0].x3 << std::endl ;

    slot slotArray[5] ;

    slotArray[0] = slot( 10, 20, 30 ) ;
    slotArray[0] = slot( 44, 55, 66 ) ;

    std::cout << slotArray[0].x1 << ":" << slotArray[0].x2 << ":" << slotArray[0].x3 << std::endl ;
}
share|improve this answer
I'm using Qt Creator. – mc360pro Jul 13 at 2:34
@mc360pro As far as I understand as long as your relatively recent in your version you should be good. – Shafik Yaghmour Jul 13 at 2:39

Why don't you use 2D array ?! if number of elements to be saved won't be equal in every cell you can use array of vector or array of list

for example

vector<int> array[5];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.