不再详细介绍,直接用代码展示用法。

具体的介绍建议去官方一点的网站看,我在网上搜罗了一大堆,大多都写的不全面,有的还有错误。

建议去这个网站看,虽然是英文的但能看懂:http://www.cplusplus.com/reference/stl/

string code:

#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream> 

using namespace std;

int main()
{
	char s[10] = "12345";
	string str;
	str.reserve(5);	//设置string里的串容量,不会填充数据.
	/* string 和 字符数组不要混合使用,否则会带来意想不到的后果 
	for(int i=0; i<5; i++)
		str[i] = s[i];
	printf("%d\n", str.size());
	printf("%d\n", str.length());
	*/
	str = "12345";
	printf("str.size() = %d\n", str.size());
	cout << str << endl;
	
	string str2("ABC", 10);	//将"ABC"存到str里,最多存储前10个字节
	cout << str2 << endl;
	
	string str3("abcdef", 2, 10); // 将"abcdef"的2号位置,做为字符串开头,存到str里.且最多存储10个字节
	cout << str3 << endl;
	
	string str4(10, 'A'); 	// 存10个'A'到str4里,第二个参数只能是单个字符 
	cout << str4 << endl;
	/*	
	string str1;
	str1.assign("ABC"); //清空string串,然后设置string串为"ABC"
 
	str1.length();  //获取字符串长度	 
	str1.size(); //获取字符串数量,等价于length()
	str1.capacity(); //获取容量,容量包含了当前string里不必增加内存就能使用的字符数
		 
	str1.resize(10);//表示设置当前string里的串大小,若设置大小大于当前串长度,则用字符\0来填充多余的.
	str1.resize(10, 'c'); //设置串大小,若设置大小大于当前串长度,则用字符c来填充多余的
	
	str.reserve(10); //设置string里的串容量,不会填充数据.
	str1.swap(str2);  //替换str1 和 str2 的字符串
	
	str1.push_back('A');  //在str1末尾添加一个'A'字符,参数必须是字符形式
	str1.append ("ABC"); //在str1末尾添加一个"ABC"字符串,参数必须是字符串形式
	 
	str1 = "ABCDEF";
	/*
	string& insert (size_t pos, const string& str);
	string& insert (size_t pos, const char* s);
	string& insert (size_t pos, const char* s, size_t n);
	string& insert (size_t pos, size_t n, char c);
	void insert (iterator p, size_t n, char c);
	iterator insert (iterator p, char c);

	str1.insert(2, "ABC"); //在str1的下标为2的位置,插入"ABC"
	str1.insert(str.begin() + 2, 'A'); // 在str1的第二个位置插入'A' , 第一个参数是迭代器 
	str1.erase(2);  //删除下标为2的位置,比如: "ABCD" --> "AB"
	str1.erase(2,1);  //从下标为2的位置删除1个,比如: "ABCD"  --> "ABD"
	str1.clear();   //删除所有
	
	basic_string& erase (size_type pos = 0, size_type len = npos);
	iterator erase (iterator p);
	iterator erase (iterator first, iterator last);
	
	str1.replace(2,4, "ABCD"); //从下标为2的位置,替换4个字节,为"ABCD"

	str1.empty();   //判断为空, 为空返回true
	*/
	
	str = "abcdef";
	cout << str << endl;
	str.insert(2, "tqq");
	cout << str << endl;
	
	str.insert(str.begin() + 1, 'z');
	cout << str << endl;
	
	str.erase(2, 1);	// 删除2号位置的元素一次 
	cout << str << endl;
	str.erase(str.begin() + 2);	// 删除2号位置的元素 
	cout << str << endl; 
	
	str.insert(2, "zcl", 6);	// 在2号位置插入"zcl",最多插入6个字节,不足补空格 
	cout << str << endl; 
	str.insert(2, 6, 'A');		// 在2号位置插入6个'A' 
	cout << str << endl;
	
	str.clear();
	str = "ABCDEFG";
	str.replace(2, 4, "abcd");
	cout << str << endl; 
	
	reverse(str.begin(), str.end());
	cout << str << endl;
	
	str.append("zcl");
	str.push_back('t');
	cout << str << endl;
	
	string::iterator it;
	it = str.begin();
	it = it + 1;
	
	
	return 0;
}

vector code:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
	vector<int> v;
	for(int i=0; i<10; i++)	v.push_back(i);
	vector<int> v1(10);
	vector<int> v2(3, 3);	// 初始化v2有三个元素都是3 
	for(int i=0; i<v2.size(); i++)
		printf("%d ", v2[i]);
	vector<int> v3(v.begin() + 1, v.end());	// 拷贝构造
	vector<int> v4(v3); 
	puts("");
	for(int i=0; i<v3.size(); i++)
		printf("%d ", v3[i]);
	printf("\n");
	vector<int>::iterator it;
	it = v.begin();
	it++;
	it = it +1;
	printf("%d\n", *it);
	
	v.insert(v.begin() + 1, 7);
	for(int i=0; i<v.size(); i++)
		printf("%d ", v[i]);
	printf("\n");
	v.insert(v.begin() + 3, 2, 9);
	for(int i=0; i<v.size(); i++)
		printf("%d ", v[i]);
	puts("");
	// 在v.begin()的位置插入另一个vector 2-4的元素 ,不包括4号元素 
	v.insert(v.begin(), v3.begin() + 2, v3.begin() + 4);
	for(int i=0; i<v.size(); i++)
		printf("%d ", v[i]);
	printf("\n");
	
	v.clear();
	for(int i=0; i<7; i++)
		v.push_back(i);
	v.erase(v.begin() + 3);	// 删除指定位置的元素 
	v.pop_back();	// 删除末尾元素 
	for(it = v.begin(); it != v.end(); ++it)
		printf("%d ", *it);
	puts("");
	v.erase(v.begin(), v.begin() + 2);
	for(int i=0; i<v.size(); ++i)
		printf("%d ", v[i]);
	printf("\n"); 
	
	printf("%d\n", v.at(2));
	printf("%d\n", v.front());
	printf("%d\n", v.back());
	
	printf("v.size = %d\n", v.size());
	v3.swap(v);
	v3.assign(2, 10);	// 设置容器的大小为2,且元素全部为10 
	printf("v3.size = %d\n", v3.size());
	printf("v.size = %d\n", v.size());
	for(int i=0; i<v3.size(); i++)
		printf("%d ", v3[i]);
	printf("\n");
	
	/* pai xu*/
	sort(v.begin(), v.end());
	
	
	return 0;	
} 

map code:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map> 

using namespace std;
int main()
{
	map<int, string> ma;
	ma.clear();
	ma.insert(pair<int, string>(1, "zcl"));
	ma.insert(map<int, string>::value_type(2, "tqq"));
	ma[3] = "123";
	printf("%d\n", ma.size()); 
	map<int, string>::iterator it;
	// it = it + 1;	// 错误,没有重载+ 
	for(it = ma.begin(); it != ma.end(); it++)
	{
		cout << it->first << "->" << it->second << endl; 
	}
	it = ma.begin();
	if(ma.count(1))
		it = ma.find(1);
	cout << it->first << "->" << it->second << endl;
	ma[4] = "abc";
	ma[5] = "def";
	it = ma.upper_bound(2);
	cout << it->first << "->" << it->second << endl;
	it = ma.lower_bound(2);
	cout << it->first << "->" << it->second << endl;
	
	ma.erase(2);	// 删除指定键值的元素 
	map<int, string> ma2;
	ma2.swap(ma);
	printf("%d\n", ma.size());
	
	 
	
	return 0;
}

set code:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map> 

using namespace std;
int main()
{
	map<int, string> ma;
	ma.clear();
	ma.insert(pair<int, string>(1, "zcl"));
	ma.insert(map<int, string>::value_type(2, "tqq"));
	ma[3] = "123";
	printf("%d\n", ma.size()); 
	map<int, string>::iterator it;
	// it = it + 1;	// 错误,没有重载+ 
	for(it = ma.begin(); it != ma.end(); it++)
	{
		cout << it->first << "->" << it->second << endl; 
	}
	it = ma.begin();
	if(ma.count(1))
		it = ma.find(1);
	cout << it->first << "->" << it->second << endl;
	ma[4] = "abc";
	ma[5] = "def";
	it = ma.upper_bound(2);
	cout << it->first << "->" << it->second << endl;
	it = ma.lower_bound(2);
	cout << it->first << "->" << it->second << endl;
	
	ma.erase(2);	// 删除指定键值的元素 
	map<int, string> ma2;
	ma2.swap(ma);
	printf("%d\n", ma.size());
	
	 
	
	return 0;
}