下面是一个C++添加简单单词字典和按字母排序实例总结代码。
该例子用到了STL容器中的set、map、vector、iterator,具体代码如下:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <set> //STL容器之一
#include <map>
#include <cctype>
using namespace std;
char toLower(char ch){ return tolower(ch); }
string & Tolower(string &st){ transform(st.begin(), st.end(), st.begin(), tolower); return st; };
void display(const string &s){ cout << s << " "; };
int main()
{
string w1 = "I am a word";
string w2 = "this is second";
string w3 = "test is start";
vector<string> words;
words.push_back(w1);
words.push_back(w2);
words.push_back(w3);
for_each(words.begin(), words.end(), display);
cout << endl;
cout << endl;
set<string> wordset; //关联容器,升序自动排序容器内的对象
transfor