Examples of nested map and vector in STL

Source: Internet
Author: User

# Pragma warning (Disable: 4786)

# Include <string>

# Include <iostream>

# Include <algorithm>

# Include <map>

# Include <vector>

Using namespace STD;

Typedef Map <string, string> string2string;

Typedef STD: Map <string, vector <string> string2vector;

 

Int main ()

{

STD: Map <string, string> map_test;

Map_test.insert (string2string: value_type ("2001", "test1 "));

Map_test.insert (string2string: value_type ("2002", "Test2 "));

Map_test.insert (string2string: value_type ("2003", "test3 "));

Map_test.insert (string2string: value_type ("2004", "test4 "));

Map_test.insert (string2string: value_type ("2005", "test5 "));

 

Map <string, string >:: const_iterator map_conitor = map_test.begin ();

For (; map_conitor! = Map_test.end (); map_conitor ++)

{

Cout <map_conitor-> first <"" <map_conitor-> second <Endl;

}

Cout <"//////////////////////////////////// /// // n ";

STD: vector <string> vec1;

Vec1.push _ back ("test1_vector1 ");

Vec1.push _ back ("test1_vector2 ");

Vec1.push _ back ("test1_vector3 ");

Vec1.push _ back ("test1_vector4 ");

STD: vector <string> vec2;

Vec2.push _ back ("test2_vector1 ");

Vec2.push _ back ("test2_vector2 ");

Vec2.push _ back ("test2_vector3 ");

Vec2.push _ back ("test2_vector4 ");

 

STD: Map <string, vector <string> testmap;

Testmap. insert (string2vector: value_type ("10001", vec1 ));

Testmap. insert (string2vector: value_type ("10002", vec2 ));

STD: Map <string, vector <string >:: iterator map_it = testmap. Begin ();

For (; map_it! = Testmap. End (); map_it ++)

{

Cout <map_it-> first <"/t ";

Vector <string >:: iterator mapvec_itor = map_it-> second. Begin ();

For (; mapvec_itor! = Map_it-> second. End (); mapvec_itor ++)

{

Cout <(* mapvec_itor) <"";

}

Cout <Endl;

}

Cout <"/N ////////////////////////////////// /// // "<Endl;

 

Map <string, vector <string >>:: iterator iter = testmap. Find ("10002 ");

If (ITER! = Testmap. End () // shoshould like this

{

Vector <string >:: iterator it = ITER-> second. Begin ();

While (it! = ITER-> second. End ())

{

Cout <(* It ++) <Endl;

}

}

Return 0;

}

# Pragma warning (Disable: 4786)

# Include <string>

# Include <iostream>

# Include <algorithm>

# Include <map>

# Include <vector>

Using namespace STD;

Typedef Map <string, string> string2string;

Typedef STD: Map <string, vector <string> string2vector;

 

Int main ()

{

STD: Map <string, string> map_test;

Map_test.insert (string2string: value_type ("2001", "test1 "));

Map_test.insert (string2string: value_type ("2002", "Test2 "));

Map_test.insert (string2string: value_type ("2003", "test3 "));

Map_test.insert (string2string: value_type ("2004", "test4 "));

Map_test.insert (string2string: value_type ("2005", "test5 "));

 

Map <string, string >:: const_iterator map_conitor = map_test.begin ();

For (; map_conitor! = Map_test.end (); map_conitor ++)

{

Cout <map_conitor-> first <"" <map_conitor-> second <Endl;

}

Cout <"//////////////////////////////////// /// // n ";

STD: vector <string> vec1;

Vec1.push _ back ("test1_vector1 ");

Vec1.push _ back ("test1_vector2 ");

Vec1.push _ back ("test1_vector3 ");

Vec1.push _ back ("test1_vector4 ");

STD: vector <string> vec2;

Vec2.push _ back ("test2_vector1 ");

Vec2.push _ back ("test2_vector2 ");

Vec2.push _ back ("test2_vector3 ");

Vec2.push _ back ("test2_vector4 ");

 

STD: Map <string, vector <string> testmap;

Testmap. insert (string2vector: value_type ("10001", vec1 ));

Testmap. insert (string2vector: value_type ("10002", vec2 ));

STD: Map <string, vector <string >:: iterator map_it = testmap. Begin ();

For (; map_it! = Testmap. End (); map_it ++)

{

Cout <map_it-> first <"/t ";

Vector <string >:: iterator mapvec_itor = map_it-> second. Begin ();

For (; mapvec_itor! = Map_it-> second. End (); mapvec_itor ++)

{

Cout <(* mapvec_itor) <"";

}

Cout <Endl;

}

Cout <"/N ////////////////////////////////// /// // "<Endl;

 

Map <string, vector <string >>:: iterator iter = testmap. Find ("10002 ");

If (ITER! = Testmap. End () // shoshould like this

{

Vector <string >:: iterator it = ITER-> second. Begin ();

While (it! = ITER-> second. End ())

{

Cout <(* It ++) <Endl;

}

}

Return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.