Can we store 2 types of data in the STL list?

I want my list to have an integer as well as a string value. is it possible?
I implement a hash table using STL lists that can only store an integer. I haveh the string to get the index where I store my integer. Now I want my string to be stored with an integer as well.

EDIT 1:
so I use this statement:

list<pair<int,string>> table[127]; 

and here is the im get error:
>>' should be →' in the nested argument list of the template OK, I fixed it. It seems that I did not put a space in "→", so now fixing it

next question
how to add a couple to an array of tables?

+4
source share
3 answers

You can have a list of std::pair or, C ++ 11, std::tuple , for example:

 std::list < std::pair< int, std::string > >list; std::list < std::tuple< int, std::string > >list; 

To access elements within a pair, use pair.first and pair.second . To access elements inside a tuple, use std::get :

 auto t = std::make_tuple(1,"something"); std::get<0>(t);//will get the first element of the tuple 
+6
source

You can use std::pair or std::tuple ,

 std::list<std::pair<int, string>> list; 
+2
source

You can save a string and an integer in a structure and save structure objects.

Each list item may look like this:

 struct element { string str; int val; }; 

This is the C way of processing, please @SingerOfTheFall answer as well.

+1
source

Source: https://habr.com/ru/post/1501997/


All Articles