STL中一些容器的使用
来源:互联网 发布:telnet指定端口 编辑:程序博客网 时间:2024/06/05 20:14
1. vector
小例子
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> iv(2, 9);
iv.push_back(1);
iv.push_back(2);
iv.push_back(3);
int i;
for (i = 0; i < (int)iv.size(); i++)
{
cout << iv[i] << ' ';
}
cout<< endl;
vector<int>::iterator ite;
for (ite = iv.begin(); ite != iv.end(); ite++)
{
cout << *ite << ' ';
}
cout << endl;
cout << "size=" << iv.size() << endl;
cout << "capacity=" << iv.capacity() << endl;
getchar();
return 0;
}
2. list
小例子
#include "stdafx.h"
#include <list>
#include <iostream>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
list<int> ilist;
int i;
for (i = 0; i < 5; i++)
{
ilist.push_back(i);
}
list<int>::iterator ite;
int key = 3;
ite = find(ilist.begin(), ilist.end(), key);
if (ite != ilist.end())
{
ilist.insert(ite, 99);
}
else
{
cout << "没有找到" << key << endl;
}
for (ite = ilist.begin(); ite != ilist.end(); ite++)
{
cout << *ite << ' ';
}
getchar();
return 0;
}
3. map
小例子
#include "stdafx.h"
#include <map>
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
map<int, string> testMap;
//增加元素
testMap.insert(make_pair(1, "a"));
testMap.insert(make_pair(2, "b"));
testMap.insert(make_pair(3, "c"));
testMap.insert(make_pair(4, "d"));
testMap.insert(make_pair(5, "e"));
map<int, string>::iterator ite;
for (ite = testMap.begin(); ite != testMap.end(); ite++)
{
cout << ite->first << " " << ite->second << endl;
}
cout << endl;
//删除元素
ite = testMap.find(3);
if (ite != testMap.end())
{
testMap.erase(ite);
}
for (ite = testMap.begin(); ite != testMap.end(); ite++)
{
cout << ite->first << " " << ite->second << endl;
}
cout << endl;
//修改元素
ite = testMap.find(5);
if (ite != testMap.end())
{
ite->second = "eee";
}
for (ite = testMap.begin(); ite != testMap.end(); ite++)
{
cout << ite->first << " " << ite->second << endl;
}
cout << endl;
//查找元素
ite = testMap.find(2);
if (ite != testMap.end())
{
cout << ite->first << " " << ite->second << endl;
}
getchar();
return 0;
}
- STL中一些容器的使用
- STL 中容器的一些简单操作
- C++ STL容器中erase的使用
- STL 中 容器 list 的使用
- STL 中容器 map 的使用
- STL中list的容器使用介绍
- STL的一些常用容器
- STL容器的使用
- 关于STL中vector容器的一些总结
- 关于STL中vector容器的一些总结
- 关于STL中deque容器的一些总结
- 多线程中使用stl容器
- 注意C++ STL容器中erase的使用
- STL中Vector容器Find的使用(泛型)
- STL中Vector容器Find的使用(int查找)
- C++ STL 中 map 容器的说明和使用技巧
- C++ STL 中 map 容器的说明和使用技巧 .
- C++ STL 中 map 容器的说明和使用技巧
- 实现网易新闻的tab切换效果
- E - Birthday Paradox Light 1104
- 匈牙利游戏(次短路问题)
- 在myeclipse利用ant构建时遇到The path xxx appears to be part of Subversion 1.7 (SVNKit 1.4) or greater解决办法
- C++ 用libcurl库进行http通讯网络编程
- STL中一些容器的使用
- 接口回调传图片
- 从零学React Native之05混合开发
- 二维元组转列表的方法
- 141.leetcode Linked List Cycle(easy)[链表是否有环 快慢指针]
- Linux命令学习:find
- HDU1527——取石子游戏(威佐夫博弈)
- 基于webkit内核webos系统架构
- 提高PHP代码的性能10条建议