STL算法
- 需要导入
#include <algorithm>
next_permutuation
- 此算法,将区间内容转换为下一种排列方式
- 排列按照字母递增的顺序进行,要得到区间内容的所有排列组合,应从最初的顺序开始,为此程序使用STL算法sort()
- 自动提供唯一的不重复的排列组合,这就是"awl"比“all"(有重复的字符)的排列组合要多的原因
//===============================================================
//FileName:
// strgst1.cpp
//Date:
// 2019/11/29
//Author:
// khoing(https://blog.csdn.net/qq_45391763)
//===============================================================
#include <iostream>
#include <string>
#include <algorithm>
int main7() {
using namespace std;
string letters;
cout << "Enter the letter grouping (quit to quit): ";
//------------------------------------------------------------------------
while (cin >> letters && letters != "quit") {
cout << "Permutations of " << letters << endl;
//排列按照字母递增的顺序进行,要得到区间内容的所有排列组合,应从最初的顺序开始,为此程序使用STL算法sort()
sort(letters.begin(), letters.end());
//自动提供唯一的不重复的排列组合,这就是"awl"比“all"(有重复的字符)的排列组合要多的原因
//------------------------------------------------------------------------
cout << letters << endl;
//------------------------------------------------------------------------
while (next_permutation(letters.begin(), letters.end()))
cout << letters << endl;
cout << "Enter next sequence (quit to quit): ";
}
cout << "Done.\n";
return 0;
}
remove、erase
成员函数remove
la.remove(4);//remove all 4 from the list
非成员函数remove
last = remove(lb.begin(), lb.end(), 4);
//lb删除后4个元素
//非成员函数,更通用,可以将它们用于数组、string对象、STL容器,还可以处理混合的的容器类型,
//不是成员,不能调整链表的长度
//将没被删除的元素放在链表的开始位置,并返回一个指向新的超尾值的迭代器。
//此时返回倒数第4个位置的值,迭代器指的值
erase
lb.erase(last, lb.end());
//第一个参数迭代器指向的开始值
//第二个参数迭代器指向的结束值
//使用erase方法删除一个区间,该区间描述了链表中不再需要的部分,左闭右开
示例
//===============================================================
//FileName:
// listrmv.cpp
//Date:
// 2019/11/29
//Author:
// khoing(https://blog.csdn.net/qq_45391763)
//===============================================================
#include <iostream>
#include <list>
#include <algorithm>
//------------------------------------------------------------------------
void Show(int);
//------------------------------------------------------------------------
const int LIM = 10;
//------------------------------------------------------------------------
int main8() {
using namespace std;
int ar[LIM] = { 4, 5, 4, 2, 2, 3, 4, 8, 1, 4 };
//------------------------------------------------------------------------
list<int> la(ar, ar + LIM);
list<int> lb(la);
//------------------------------------------------------------------------
cout << "Original list contents:\n\t";
for_each(la.begin(), la.end(), Show);
cout << endl;
//------------------------------------------------------------------------
la.remove(4);//使用成员函数,可以将链表中所有值为4的元素都删除,同时链表的长度将自动调整
cout << "After using the remove() method:\n";
cout << "la:\t";
for_each(la.begin(), la.end(), Show);
cout << endl;
//------------------------------------------------------------------------
list<int>::iterator last;
last = remove(lb.begin(), lb.end(), 4);//删除后4个
//非成员函数,更通用,可以将它们用于数组、string对象、STL容器,还可以处理混合的的容器类型,
//不是成员,不能调整链表的长度
//将没被删除的元素放在链表的开始位置,并返回一个指向新的超尾值的迭代器。
//此时返回倒数4个位置
cout << "After using the remove() function:\n";
cout << "lb:\t";
for_each(lb.begin(), lb.end(), Show);
cout << endl;
//------------------------------------------------------------------------
lb.erase(last, lb.end());
//第一个参数迭代器指向的开始值
//第二个参数迭代器指向的结束值
//使用erase方法删除一个区间,该区间描述了链表中不再需要的部分,左闭右开
cout << "After using the erase() method:\n";
cout << "lb:\t";
for_each(lb.begin(), lb.end(), Show);
cout << endl;
return 0;
}
//------------------------------------------------------------------------
void Show(int v)
{
std::cout << v << ' ';
}
tolower、count
tolower
char toLower(char ch) {
char a = tolower(ch);//返回的int类型的ascci码,转为对应的字符
return a;
//int tolower(int c);
//c -- 这是要被转换为小写的字母。
//如果 c 有相对应的小写字母,则该函数返回 c 的小写字母,否则 c 保持不变。
//返回值是一个可被隐式转换为 char 类型的 int 值。
/*
char a = 'A';
cout << tolower(a) << endl; 返回97
cout << (char)tolower(a) << endl; 返回a
*/
}
count
count(words.begin(), words.end(), *si);//前两个参数是区间,第三个参数是值,返回这个值在区间内出现的次数
示例
//===============================================================
//FileName:
// usealgo.cpp
//Date:
// 2019/11/30
//Author:
// khoing(https://blog.csdn.net/qq_45391763)
//===============================================================
//using several STL elements
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <map>
using namespace std;
//------------------------------------------------------------------------
char toLower(char ch);
string& ToLower(string& st);
void display(const string& s);
int main() {
vector<string> words;
//------------------------------------------------------------------------
cout << "Enter words (enter quit to quit):\n";
string input;
while (cin >> input && input != "quit")
words.push_back(input);
//------------------------------------------------------------------------
cout << "You entered the following words:\n";
for_each(words.begin(), words.end(), display);
cout << endl;
//------------------------------------------------------------------------
// place words in set, converting to lowercase
set<string> wordset;
transform(words.begin(), words.end(),
insert_iterator<set<string> >(wordset, wordset.begin()),
ToLower);//这里使用自定义的函数,因为tolower默认返回的是int
cout << "\nAlphabetic list of words:\n";
for_each(wordset.begin(), wordset.end(), display);
cout << endl;
//------------------------------------------------------------------------
// place word and frequency in map
map<string, int> wordmap;
set<string>::iterator si;
for (si = wordset.begin(); si != wordset.end(); si++)
wordmap[*si] = count(words.begin(), words.end(), *si);//前两个参数是区间,第三个参数是值,返回这个值在区间内出现的次数
//------------------------------------------------------------------------
// display map contents
cout << "\nWord frequency:\n";
for (si = wordset.begin(); si != wordset.end(); si++)
cout << *si << ": " << wordmap[*si] << endl;
return 0;
}
//------------------------------------------------------------------------
void display(const string& s)
{
cout << s << " ";
}
//------------------------------------------------------------------------
char toLower(char ch) {
char a = tolower(ch);//返回的int类型的ascci码,转为对应的字符
return a;
//int tolower(int c);
//c -- 这是要被转换为小写的字母。
//如果 c 有相对应的小写字母,则该函数返回 c 的小写字母,否则 c 保持不变。
//返回值是一个可被隐式转换为 char 类型的 int 值。
/*
char a = 'A';
cout << tolower(a) << endl; 返回97
cout << (char)tolower(a) << endl; 返回a
*/
}
//------------------------------------------------------------------------
string& ToLower(string& st)
{
transform(st.begin(), st.end(), st.begin(), toLower);
return st;
}