1.标准模板库也就是STL(Standard Template Library). 提供了:存储信息的容器,访问容器存储信息的迭代器,操作容器内容的算法
2.容器是用于存储数据的STL类,STL提供了两种类型的容器类:顺序容器和关联容器
3.顺序容器是按顺序存储,插入速度快,但是操作和查找比较慢
4.STL的顺序容器:std::vector类似数组,在最后插入数据, std:deque类似数组,可以在前后插入数据, std::list类似链表
5.关联容器,插入比较慢,但是查询快
6.STL的关联容器:std::set里面的数据是排序的, std::map存储健-值对,根据唯一键将键值对排序, std::multiset和set一样,但是可以存储多个相同的值, std::multimap和map一样,但是可以存储多个键一样的值
7.STL迭代器,最简单的迭代器是指针,给出容器的指针,根据顺序访问容器中的值。STL中的迭代器是模版类,也就是泛型指针。
8.STL算法。STL提供了很多常用的算法。:std::find在集合众查找, std::find_if根据指定的条件查找, std::reverse反转, std::remove_if根据条件删除, std::transform变换
9.
vector <int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
vector <int>::iterator i = a.begin();
while(i!=a.end())
{
cout << *i << endl;
i++;
}
vector <int>::iterator ie = find(a.begin(), a.end(), 2);
if(ie!=a.end())
{
int n = distance(a.begin(), ie);
}
上面的额程序就是创建了一个整型vector a。然后对a在尾部分别出入1 2 3,然后创建一个迭代器,只想a的开头,然后逐一迭代输出。然后对a进行查找只为2的元素,并把查找到的位置给新定义的迭代器。然后用distance计算出离a的开始的位置。其中begin指向的是a的开头的元素,end指向的是a的最后一个元素的后面
10.在c++中对字符操作比较繁琐,所以定义了一个STL的string类。这个类包括了最常用的字符串操作的功能:复制,连接,查找,截断,反转,大小写等。如果用这个类,就要包含头文件<string>
11.string类提供了我们用到的所有的构造函数。
const char * a = "hello world";
std::string b(a);
std::string b = a;
std::string b("hello world");
std::string c(b);
std::string b(a, 4);//只把a前面的4个字符给b,用来初始化
std::string b(10,'x');//用10个x来初始化b
12.string内容访问
string a("hello world");
for(size_t n = 0; n < a.length(), n++)
{
cout << a[n] << endl;
}
string::const_iterator i;
for(i=a.begin(); i!=a.end(); i++)
{
cout << *i << endl;
}
cout << a.c_str() << endl;
上面的程序是定义了一个string类a,然后赋值hello world,然后用下标一个个访问a里面的内容,然后用迭代器i访问a里面的内容,然后用c_str()方法把a里面的全部内容一次输出
13.字符串连接。
string a("hello");
string b("world");
a += b;
string c("fun");
a.append(c);
const char * d = "will";
a.append(d);
上面显示了string类的几种字符串拼接的方法。一种是直接用+来实现,然后是用append函数来实现
14.查找字符或是字符串
string a("good day string. today is beautiful");
size_t offset = a.fine("day", 0);
if(offset != string::npos)
{
cout << "not find" << endl;
}
offset++;
size_t n = a.fine('a', offset);
上面的代码就是通过find函数的其中一个方法来寻找字符串或字符.先声明一个string的变量a,然后从a的第0个索引(也就是第一个)位置开始寻找字符串day。这个函数会返回
一个索引位置,如果没找到就返回string::npos。后面是把索引位置加1,从这个位置开始寻找字母a
15.字符串截断
string a("hello string. wake up to a beautiful day");
a.erase(13, 28);
string::iterator i = find(a.begin(), a.end(), 's');
if(i != a.end())
{
a.erase(i);
}
a.erase(a.begin(), a.end());
上面的程序是生命一个a,然后从第13个字符开始,向后删除28个字符。然后找到剩下的字符串里面s的位置,然后删除这个s,然后把字符串从最开始到最后全部删除
16.字符串反转
reverse(a.begin(), a.end());
把a从开头到结尾范围内的字符串反转
17.字符串大小写转换
string a;
getline(cin, a);
transform(a.begin(0, a.end(), a.begin(), toupper);
上面的程序是先定义一个string的变量a,然后把输入的所有字符从cin存到a中,然后用transform来转换,第一个参数是原字符串的起始位置,第二个参数是结束的位置,第三个参数是要写入字符串的开始位置,第四个参数是对字符串的操作