help!!!

how i can make a list ,that has(1,2,3,4) ?
i want this because i want to make a quiz with possibly answers!!!


char array4[4]={1:'apw',2:'ddaase',3:'2',4:'5'};
Use an associative array; for instance std::map<>
http://www.cplusplus.com/reference/stl/map/

1
2
3
4
5
6
7
8
9
10
#include <map>
#include <string>
#include <iostream>

int main()
{
    std::map< int, std::string > associative_array =
                     { { 1, "apw" }, { 2, "ddaase" }, { 3, "2" }, { 4, "5" } } ;
    std::cout << associative_array[2] << '\n' ;
}
1
2
3
4
5
6
7
8
9
10
11
#include <map>
#include <string>
#include <iostream>
using namespace std;

int main()
{

    map<int,string> a = { { 1, "apw" }, { 2, "ddaase" }, { 3, "2" }, { 4, "5" } } ;
    cout << a[2] ;
}


it doesnt work,why?
That will only work if you use C++11. If you don't, you can do it this way instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <map>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	map<int,string> a;
	a[1] = "apw";
	a[2] = "ddaase";
	a[3] = "2";
	a[4] = "5";
	cout << a[2];
}
Last edited on
it runs,but i dont want to be in output(apwddaasee25)
but something like this(1:apw,2:ddasse,3:2,4:5) ,so what should do?
thank you my friend!!!
i founded it.look!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <map>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	map<int,string> a;
	a[1] = "1:apw\t";
	a[2] = "2:ddaase\t";
	a[3] = "3:2\t";
	a[4] = "4:5\t";
	for(int i=0;i<5;i++)
	cout << a[i];
}
A more flexible approach would be to do the formatting during the print operation:
1
2
3
4
5
6
7
8
9
map<int,string> a;
a[1] = "apw";
a[2] = "ddaase";
a[3] = "2";
a[4] = "5";

for( int i = 1 ; i < 5 ; ++i )
    cout << i << " : " << a[i] << ",    " ;
cout << '\n' ;

Topic archived. No new replies allowed.