std::map的简单使用例子
2008-08-25 19:08
357 查看
std::map<double, int> map_test;
{
double a1 = 0.12;
double a2 = 0.12;
double a3 = 0.19;
std::map<double, int>::iterator it;
if ( (it = map_test.find(a1)) != map_test.end() )
{
(*it).second ++;
}
else map_test.insert( make_pair(a1, 0) );
if ( (it = map_test.find(a2)) != map_test.end() )
{
(*it).second ++;
}
else map_test.insert( make_pair(a2, 0) );
if ( (it = map_test.find(a3)) != map_test.end() )
{
(*it).second ++;
}
else map_test.insert( make_pair(a3, 0) );
}
cout << map_test.size() << endl;
for ( map<double,int>::iterator it = map_test.begin(); it != map_test.end();
++ it )
{
cout << it->first << " " << it->second << endl;
}
map<double, int> map_test2;
map_test2.insert( make_pair(13.1, 13));
map_test2.insert( make_pair(14.1, 23));
map_test2.insert( make_pair(15.1, 33));
map_test2.insert( make_pair(16.1, 43));
map_test2.insert( make_pair(17.1, 53));
// 取最后一个元素
reverse_iterator<std::map<double, int>::iterator> it_reverse = map_test2.rbegin();
cout << it_reverse->first << " " << it_reverse->second << endl ;
/////////////////////////////////////////////////////
struct DataPoint
{
DataPoint(double in_x, double in_y ) : x(in_x), y(in_y) {}
double x;
double y;
};
map<double, vector<DataPoint> > test_map;
vector<DataPoint> dataPoints;
dataPoints.push_back(DataPoint(10, 23));
dataPoints.push_back(DataPoint(11, 22));
dataPoints.push_back(DataPoint(14, 21));
test_map.insert( make_pair(.89, dataPoints));
map<double, vector<DataPoint> >::iterator it_look = test_map.find(0.89);
if ( it_look != test_map.end() )
{
it_look->second.push_back( DataPoint(89, 98));
}
map<double, vector<DataPoint> >::iterator it = test_map.begin();
for ( ; it != test_map.end(); ++ it )
{
vector<DataPoint> & dataPoints = it->second;
for ( int nPos= 0; nPos < dataPoints.size(); nPos ++ )
{
cout << dataPoints[nPos].x << " " << dataPoints[nPos].y << endl;
}
}
{
double a1 = 0.12;
double a2 = 0.12;
double a3 = 0.19;
std::map<double, int>::iterator it;
if ( (it = map_test.find(a1)) != map_test.end() )
{
(*it).second ++;
}
else map_test.insert( make_pair(a1, 0) );
if ( (it = map_test.find(a2)) != map_test.end() )
{
(*it).second ++;
}
else map_test.insert( make_pair(a2, 0) );
if ( (it = map_test.find(a3)) != map_test.end() )
{
(*it).second ++;
}
else map_test.insert( make_pair(a3, 0) );
}
cout << map_test.size() << endl;
for ( map<double,int>::iterator it = map_test.begin(); it != map_test.end();
++ it )
{
cout << it->first << " " << it->second << endl;
}
map<double, int> map_test2;
map_test2.insert( make_pair(13.1, 13));
map_test2.insert( make_pair(14.1, 23));
map_test2.insert( make_pair(15.1, 33));
map_test2.insert( make_pair(16.1, 43));
map_test2.insert( make_pair(17.1, 53));
// 取最后一个元素
reverse_iterator<std::map<double, int>::iterator> it_reverse = map_test2.rbegin();
cout << it_reverse->first << " " << it_reverse->second << endl ;
/////////////////////////////////////////////////////
struct DataPoint
{
DataPoint(double in_x, double in_y ) : x(in_x), y(in_y) {}
double x;
double y;
};
map<double, vector<DataPoint> > test_map;
vector<DataPoint> dataPoints;
dataPoints.push_back(DataPoint(10, 23));
dataPoints.push_back(DataPoint(11, 22));
dataPoints.push_back(DataPoint(14, 21));
test_map.insert( make_pair(.89, dataPoints));
map<double, vector<DataPoint> >::iterator it_look = test_map.find(0.89);
if ( it_look != test_map.end() )
{
it_look->second.push_back( DataPoint(89, 98));
}
map<double, vector<DataPoint> >::iterator it = test_map.begin();
for ( ; it != test_map.end(); ++ it )
{
vector<DataPoint> & dataPoints = it->second;
for ( int nPos= 0; nPos < dataPoints.size(); nPos ++ )
{
cout << dataPoints[nPos].x << " " << dataPoints[nPos].y << endl;
}
}
相关文章推荐
- mybaits的Mapper动态代理的简单例子(定义类型别名,ResultMap类型的使用)
- 海量数据处理实例--几个使用bit-map的简单例子
- std::hash_map的使用
- 97datepicker使用简单例子
- C++中std::map的使用
- 使用OpenMP的一个简单例子
- stl std::map容器排序及使用注意事项
- Tomcat下使用Jfreechart 简单例子(servlet)
- 使用 CXF 做 webservice 简单例子
- 使用 CXF 做 webservice 简单例子
- web.config文件自定义配置节的使用方法的一个简单例子
- python 使用 talib 和 numpy 的简单例子
- c++ com 在c#项目里面使用简单例子
- std::map 使用注意事项
- J2EE实践第四部分-使用SAP CAF实现功能的简单小例子
- 一个简单的MapView例子。气球坠落
- 一个java生成与使用WebService的简单例子
- XDownPage2.0使用简单例子一
- 学习使用Robot Framework自动化测试框架(三)——简单组合测试例子
- Linux下,使用C/C++编写"静态链接库"的一个简单例子