0

I have a set of three values pairs that I want to store in an array, each value with different type.

[string][int][double] for storing things like [John][12][40.025] [Amy][20][16000.411] and I want to be able to retrieve the values by location. What container should I use?

I have looked through many answers but most of them were about two data types, and I don't know which one better suit my case.

2
  • 2
    You can use std::tuple<string,int,double>, and get<N> to access the Nth tuple element. You can store these tuples in an array or vector. Commented Jun 17, 2020 at 14:18
  • 1
    You could also use std::vector<mystruct> where mystuct is a class or struct that contains your 3 different types. Commented Jun 17, 2020 at 14:23

2 Answers 2

3

This is what classes are for:

struct data {
    std::string name;
    int         name_describing_this_variable;
    double      another_descriptive_name;
};

You can store instances of this class in an array:

data arr[] {
    {"John", 12, 40.025},
    {"Amy",  20, 16000.411},
};
Sign up to request clarification or add additional context in comments.

5 Comments

How do I add a new set to the array?
@reddy Nothing can be added into an array. An array is initialised with the elements that it will have.
Using std::vector<data> instead of the array that has a fixed size at compile time can solve that.
What other container can I use that it can start out empty and I can add new set to it?
@drescherjm Okay I got it, this looks the easiest for me, thank you guys.
2

Use the class template std::tuple declared in the header <tuple>. For example

#include <iostream>
#include <string>
#include <tuple>

int main() 
{
    std::tuple<std::string, int, double> t ={ "John", 12, 40.025 };

    std::cout << std::get<0>( t ) << ", "
              << std::get<1>( t ) << ", "
              << std::get<2>( t ) << '\n';

    std::cout << std::get<std::string>( t ) << ", "
              << std::get<int>( t ) << ", "
              << std::get<double>( t ) << '\n';

    return 0;
}

The program output is

John, 12, 40.025
John, 12, 40.025

And you may use a container of elements with this type as for example an array because the listed types in the tuple are default constructible.

For example

#include <iostream>
#include <string>
#include <tuple>

int main() 
{
    std::tuple<std::string, int, double> t[1];

    t[0] = { "John", 12, 40.025 };

    std::cout << std::get<0>( t[0] ) << ", "
              << std::get<1>( t[0] ) << ", "
              << std::get<2>( t[0] ) << '\n';

    std::cout << std::get<std::string>( t[0] ) << ", "
              << std::get<int>( t[0] ) << ", "
              << std::get<double>( t[0] ) << '\n';

    return 0;
}

As an alternative you can define your own compound type (class or structure) that will contain sub-objects of required types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.