void main() { // 1. Create a map of ints to strings INT2STRING theMap; INT2STRING::iterator theIterator; string theString = ""; int index;
// Fill it with the digits 0 - 9, each mapped to its string counterpart // Note: value_type is a pair for maps... theMap.insert(INT2STRING::value_type(0,"Zero")); theMap.insert(INT2STRING::value_type(1,"One")); theMap.insert(INT2STRING::value_type(2,"Two")); theMap.insert(INT2STRING::value_type(3,"Three")); theMap.insert(INT2STRING::value_type(4,"Four")); theMap.insert(INT2STRING::value_type(5,"Five")); theMap.insert(INT2STRING::value_type(6,"Six")); theMap.insert(INT2STRING::value_type(7,"Seven")); theMap.insert(INT2STRING::value_type(8,"Eight")); theMap.insert(INT2STRING::value_type(9,"Nine"));
// Read a Number from the user and print it back as words for( ; ; ) { cout << "Enter \"q\" to quit, or enter a Number: "; cin >> theString; if(theString == "q") break; // extract each digit from the string, find its corresponding // entry in the map (the word equivalent) and print it for(index = 0; index < theString.length(); index++){ theIterator = theMap.find(theString[index] - '0'); if(theIterator != theMap.end() ) // is 0 - 9 cout << (*theIterator).second << " "; else // some character other than 0 - 9 cout << "[err] "; } cout << endl; } }
#include <vector>
#include <string>
#include <iostream>
#include <utility>
using namespace std;int main(void)
{
vector<string> colFirst;
map<int, string> colSecond;
colFirst.push_back("Beijing");
colFirst.push_back("Shanghai");
colFirst.push_back("Tianjin");
colSecond.insert(make_pair<int, string>(0, "Beijing"));
colSecond.insert(make_pair<int, string>(2, "Tianjin"));
colSecond.insert(make_pair<int, string>(1, "Shanghai")); unsigned i;
for (i = 0; i < 3; i++)
{
cout << colFirst[i] << " = " << colSecond[i] << endl;
}
cout << "Wish you good luck!" << endl;
}
#include <vector>
#include <set>
#include <iterator>
#include <functional>
#include <algorithm>
struct myStruct {
int sort_id;
bool operator < (const myStruct& mys) const {
return sort_id < mys.sort_id;
}
};
struct myLess {
bool operator () (const myStruct& mys1, const myStruct& mys2) const {
return mys1.sort_id < mys2.sort_id;
}
};
int main(int argc,char** argv) {
using namespace std;
myStruct mys[] = { 7,6,5,4,3,2,1,0 };
vector<myStruct> myVec(mys,mys+8);
set<myStruct> mySet(mys,mys+8);
sort(myVec.begin(),myVec.end(),myLess());
vector<myStruct>::iterator BEG1(myVec.begin()),END1(myVec.end());
for(; BEG1 != END1; ++BEG1) {
cout << BEG1->sort_id << ' ';
}
cout << endl;
set<myStruct>::iterator BEG2(mySet.begin()),END2(mySet.end());
for(; BEG2 != END2; ++BEG2) {
cout << BEG2->sort_id << ' ';
}
cout << endl;
return 0; // make vc++ happy
}
//
// Compile options needed: None
//
// <filename> : main.cpp
//
// Functions:
//
// end
// find
// insert
//////////////////////////////////////////////////////////////////////
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<int, string, less<int> > INT2STRING;
void main()
{
// 1. Create a map of ints to strings
INT2STRING theMap;
INT2STRING::iterator theIterator;
string theString = "";
int index;
// Fill it with the digits 0 - 9, each mapped to its string counterpart
// Note: value_type is a pair for maps...
theMap.insert(INT2STRING::value_type(0,"Zero"));
theMap.insert(INT2STRING::value_type(1,"One"));
theMap.insert(INT2STRING::value_type(2,"Two"));
theMap.insert(INT2STRING::value_type(3,"Three"));
theMap.insert(INT2STRING::value_type(4,"Four"));
theMap.insert(INT2STRING::value_type(5,"Five"));
theMap.insert(INT2STRING::value_type(6,"Six"));
theMap.insert(INT2STRING::value_type(7,"Seven"));
theMap.insert(INT2STRING::value_type(8,"Eight"));
theMap.insert(INT2STRING::value_type(9,"Nine"));
// Read a Number from the user and print it back as words
for( ; ; )
{
cout << "Enter \"q\" to quit, or enter a Number: ";
cin >> theString;
if(theString == "q")
break;
// extract each digit from the string, find its corresponding
// entry in the map (the word equivalent) and print it
for(index = 0; index < theString.length(); index++){
theIterator = theMap.find(theString[index] - '0');
if(theIterator != theMap.end() ) // is 0 - 9
cout << (*theIterator).second << " ";
else // some character other than 0 - 9
cout << "[err] ";
}
cout << endl;
}
}
要使用vector必须包含相关头文件#include <vector>
定义方法如下:
vector<类型名> 对象名称;
例如vector<int> vecInt;
类型名可以是自定义的类型。
map关联容器,其增加和删除节点对迭代器的影响很小。
对于迭代器来说,可以修改实值value,而不能修改key。
其迭代器iterator->first,指向key值。iterator->second,指向实值value
其头文件#include <map>
定义方法如下:
map<key,value> 对象名称;
key 和 value可以是任意你需要的类型。
vector和map标准库提供了许多的操作,具体可查看MSDN。
下面是它们的一些方法的简单应用。
#pragma warning(disable:4786)#include <vector>
#include <iostream>
#include <map>
#include <string>using namespace std;void main()
{
typedef vector<int> vecInt;
vecInt vec;
int size = vec.size();//计算元素个数此时为0
for(int i = 0; i < 10; i++)
vec.push_back(i+1);//在末尾添加元素
size = vec.size();//计算元素个数此时为10
vecInt::iterator it;//定义迭代器
for(it = vec.begin();it != vec.end(); it++)//利用迭代器进行遍历,输出所有元素
cout<<*it<<' ';
cout<<endl;
vec.erase(vec.begin()+2);//删除第三个元素
vec.insert(vec.begin()+2,99);//在第三个位置插入元素99
vec.erase(vec.begin(),vec.end());//删除从第一个开始到最后的元素,此时与vec.clear()效果一样
if(vec.empty())
cout<<"evc is empty!"<<endl; cout<<"==================================="<<endl; map<int ,string > mapStr;
typedef map<int ,string >::value_type valueType;
mapStr.insert(valueType(1,"Id"));//往map容器中插入元素
mapStr.insert(valueType(2,"Name"));
mapStr.insert(valueType(3,"Age"));
mapStr.insert(valueType(4,"Sex")); map<int ,string >::iterator itMap;//定义迭代器 for(itMap = mapStr.begin(); itMap != mapStr.end(); itMap++)//输出所有元素
cout<<itMap->first<<'\t'<<itMap->second<<endl; if((itMap=mapStr.find(2))==mapStr.end())//查找key值为2的元素未查找到返回mapStr.end()
cout<<"Not find!";
else
cout<<"Find!"<<itMap->first<<'\t'<<itMap->second<<endl;}
类的内部保存了三个指针m_start,用于指向每个元素
m_finish,用于指向最后一个元素的下一个元素位置。
m_end_of_storage,用于指向容器中最后个元素的位置。
注意:vector首先是先预分配一段空间,然后向这段空间中存放一些数据,因此m_finish指向最后个含有数据的元素的下一个元素位置
这个类提供了一个push_back方法,用于将元素插入到容器的末尾,这是一种后向插入,但vector不提供前向插入功能。
vector提供了首元素的弹出方法pop_back,它能删除第一个元素
http://blog.sina.com.cn/s/blog_5cf5e7c40100boiu.htmlmap映照容器所处理的元素数据,与数据库表的具有键值的记录非常相似,由一个键值和其他若干数据(映射数据)组成,键值和映照数据之间,可建立一个数学上的映照关系。容器的数据结构采用红黑树进行管理,插入的元素键值不允许重复,所使用的节点元素的比较函数,只对元素的键值进行比较,元素的各项数据可通过键值检索出来。
map容器中每个对象封装成pair的结构对象,它的格式形如:
pair<key,value>,
其中:key是键值,value是键值对就的数据
对于迭代器来说,可以修改实值value,而不能修改key。
其迭代器iterator->first,指向key值。iterator->second,指向实值value
http://www.kuqin.com/cpluspluslib/20071231/3264.html