STL中map 和vector巢狀使用示例

2021-05-21 18:21:18 字數 3580 閱讀 2727

#pragma warning(disable : 4786 )    

#include

#include

#include

#include

#include

using namespace std;   

typedef mapstring2string;   

typedef std::map> string2vector;   

int main()   

std::mapmap_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::const_iterator map_conitor = map_test.begin();   

for(; map_conitor!= map_test.end(); map_conitor++)   

coutstd::vectorvec1;   

vec1.push_back("test1_vector1");   

vec1.push_back("test1_vector2");   

vec1.push_back("test1_vector3");   

vec1.push_back("test1_vector4");   

std::vectorvec2;   

vec2.push_back("test2_vector1");   

vec2.push_back("test2_vector2");   

vec2.push_back("test2_vector3");   

vec2.push_back("test2_vector4");   

std::map> testmap;   

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

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

std::map>::iterator map_it = testmap.begin();   

for ( ; map_it != testmap.end(); map_it++)   

coutvector::iterator mapvec_itor = map_it->second.begin();   

for ( ; mapvec_itor !=  map_it->second.end(); mapvec_itor++)   

cout<<(*mapvec_itor)<<" ";   

cout::iterator iter=testmap.find( "10002" );   

if(iter!=testmap.end()) //should like this   

vector::iterator it = iter->second.begin();   

while(it != iter->second.end())   

cout<<(*it++)#pragma warning(disable : 4786 ) 

#include

#include

#include

#include

#include

using namespace std;

typedef mapstring2string;

typedef std::map> string2vector;

int main()

std::mapmap_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::const_iterator map_conitor = map_test.begin();

for(; map_conitor!= map_test.end(); map_conitor++)

coutstd::vectorvec1;

vec1.push_back("test1_vector1");

vec1.push_back("test1_vector2");

vec1.push_back("test1_vector3");

vec1.push_back("test1_vector4");

std::vectorvec2;

vec2.push_back("test2_vector1");

vec2.push_back("test2_vector2");

vec2.push_back("test2_vector3");

vec2.push_back("test2_vector4");

std::map> testmap;

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

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

std::map>::iterator map_it = testmap.begin();

for ( ; map_it != testmap.end(); map_it++)

coutvector::iterator mapvec_itor = map_it->second.begin();

for ( ; mapvec_itor !=  map_it->second.end(); mapvec_itor++)

cout<<(*mapvec_itor)<<" ";

cout::iterator iter=testmap.find( "10002" );

if(iter!=testmap.end()) //should like this

vector::iterator it = iter->second.begin();

while(it != iter->second.end())

cout<<(*it++)

STL中vector和map的查詢和刪除

目前vector 和map 是stl 中最常用的兩個容器,在使用 vector 或map 時,根據需要會在容器中查詢,然後刪除,下面總結一下實際測試中正確的結果。查詢 map的查詢可是使用 find 函式。map int,cstring iterator iter iter mapintstring...

STL中map和priority queue的應用

基本的知識就不再講了,只列下用map做的題目 題1 poj 1002 487 3279 這個題目煩的死,tle了n次,只是因為我用g 提交的,好啦,要注意的就是把陣列開大點,和用c語言輸入,最後如果沒有重複的 號碼輸出 no duplicates.include include include in...

STL中的map和multimap小結

1 使用map multimap之前必須包含標頭檔案 include並且和所有的關聯式容器一樣,map multimap通常以平衡二叉樹來完成 2 namespacestd 第乙個template引數被當作元素的key,第二個當作元素的value。key value必須具備assignable和co...