泛型算法

来源:互联网 发布:网络电视直播哪个好 编辑:程序博客网 时间:2024/06/04 08:09

这是一个读书笔记。

函数参数可以是其他容器的迭代器。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

只读算法:

算法一:

在某种容器中查找一个特定的元素,返回指向第一个给定值的元素的迭代器。如果范围中无匹配元素,则返回第二个参数来表示搜索失败

#include<algorithm>

find(起始迭代器,结束迭代器,查找元素)

例子:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int a[] = {2, 2, 3, 4, 5, 6, 7};
vector<int>b(a, a+7);
vector<int>::const_iterator c = find(b.cbegin(), b.cend(), 5);
cout<< "The value 5 " << (c == b.cend() ? "is not present" : "is present")<< endl;
return 0;
}


算法二:

返回定值在序列中出现的次数

#include<algorithm>

count(起始迭代器, 结束迭代器, 定值)

例子:

#include<iostream>

#include<vector>

#include<algorithm>

using namespace std;

int main()

{

    int a[] = {1, 2, 3, 4, 5, 5 ,6, 5, 7,5};

    vector<int> b(a, a+10);

     int c= count(b.begin(), b.end(), 5);

     cout<< c << endl;

return 0;

}


算法三:

对给定范围的元素求和

#include<numeric>

accumulate(起始迭代器, 结束迭代器, 和的初值)

和的初值决定了函数中使用哪儿加法运算符以及返回值类型

例子:

#include<iostream>
#include<vector>
#include<string>
#include<numeric>
using namespace std;
int main()
{
int a[] = {2, 2, 3, 4, 5, 6, 7};
int b = accumulate(begin(a), end(a), 0);
cout << b << endl;
string c[] = {"124", "abc"};
vector<string> d(c, c+2);
string e = accumulate(d.cbegin(), d.cend(), string(""));
cout << e << endl;
return 0;
}


算法四:

确定两个序列是否保存相同的值,如果所有对应元素都相等,则返回true,否则返回false

#include<algorithm>

equal(序列一起始迭代器,序列一结束迭代器,序列二起始迭代器)

序列二至少与序列一一样长

例子:

#include<iostream>
#include<vector>
#include<list>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string a[] = {"123", "568", "Adbd"};
vector<string> b(a, a + 3);
char * c[] = {"235", "345", "456"};
list<char *> d(c, c+3);
bool e = equal(b.cbegin(), b.cend(), d.cbegin());
cout << e << endl;
return 0;
}


写入算法:

算法五:

将一个给定值赋予输入序列中的每一个元素

#include<algorithm>

fill(起始迭代器,结束迭代器,定值)

例子:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int a[] = {1, 2, 3, 4, 5};
vector<int>b(a, a+5);
fill(b.begin(), b.begin() + b.size() / 2 , 0);
for(int i = 0; i < b.size(); ++i)
cout << b[i] << ends;
cout << endl;
return 0;
}


算法六:

将给定值赋予迭代器指向的元素开始的指定个元素

#include<algorithm>

fill_n(起始迭代器, 个数,定值)

例子1:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int>b;
b.reserve (20);
b.resize(10);
fill_n(b.begin(), 10, 10);
for(int i = 0; i < b.size(); ++i)
cout << b[i] << ends;
cout << endl;
return 0;
}

例子2:

#include<iostream>
#include<iterator>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int>vec;
fill_n(back_inserter(vec), 10, 10);
for(int i = 0; i < vec.size(); ++i)
cout << vec[i] << ends;
cout << endl;
return 0;
}


算法七:

向目的迭代器指向的的输出序列中的元素写入数据,并返回目的位置迭代器(递增后)的值

#include<algorithm>

copy(序列一起始迭代器,序列一结束迭代器,目的序列起始迭代器)

目的序列至少要包含于序列一一样多的元素

例子:

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int a2[sizeof(a1)/sizeof(*a1)];
int * ret = copy(begin(a1), end(a1), a2);
for(int i = 0 ; i < 11; ++i)
cout << a2[i] << ends;
cout << endl;
return 0;
}


算法八:

将序列中等于定值的元素改为另一个元素

#include<algorithm>

replace(起始迭代器, 结束迭代器, 定值, 替换值)

例子:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int a[] = {1,2 ,3 ,4 ,5, 4};
vector<int>b(a, a+6);
replace(b.begin(), b.end(), 4, 7);
for(int i = 0; i < b.size(); ++i)
cout << b[i] << ends;
cout << endl;
return 0;
}


算法九:

将序列中等于定值的元素改为另一个元素,并保存在另一个序列中

#include<algorithm>

replace_copy(序列一起始迭代器, 序列一结束迭代器,序列二保存迭代器,定值,替换值)

例子:

#include<iostream>
#include<iterator>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int a[] = {1,2 ,3 ,4 ,5, 4};
vector<int>b(a, a+6);
vector<int>d;
replace_copy(b.begin(), b.end(), back_inserter(d), 4, 7);
for(int i = 0; i < b.size(); ++i)
cout << b[i] << ends;
cout << endl;
for(int j = 0; j < d.size(); ++j)
cout << d[j] << ends;
cout << endl;
return 0;
}


排序算法:

算法十:

重排输入序列中的元素

#include<algorithm>

sort(起始迭代器,结束迭代器)

sort(起始迭代器,结束迭代器,序关系函数)

例子:

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}


int main()
{
string a[] = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
vector<string> b(a, a+10);
for(int z = 0; z < b.size(); ++z)
cout << b[z] << ends;
cout << endl;
sort(b.begin(), b.end(), isShorter);
for(int i = 0; i < b.size(); ++i)
cout<< b[i] << ends;
cout << endl;
return 0;
}


算法十一:

重排序列,使不重复的元素出现在序列的开始部分,并返回一个指向不重复值范围末尾的迭代器

#include<algorithm>

unique(起始迭代器,结束迭代器)

使用前先排序

例子:

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int main()
{
string a[] = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
vector<string> b(a, a+10);
for(int z = 0; z < b.size(); ++z)
cout << b[z] << ends;
cout << endl;
sort(b.begin(), b.end());
for(int i = 0; i < b.size(); ++i)
cout<< b[i] << ends;
cout << endl;
vector<string>::iterator c = unique(b.begin(), b.end());
b.erase(c, b.end());
for(int j = 0; j < b.size(); ++j)
cout << b[j] << ends;
cout << endl;
return 0;
}

算法十二:

具有相同长度的元素按字典序排列

#include<algorithm>

stable_sort(起始迭代器, 结束迭代器,序关系函数)

例子:

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}


int main()
{
string a[] = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
vector<string> b(a, a+10);
for(int z = 0; z < b.size(); ++z)
cout << b[z] << ends;
cout << endl;
sort(b.begin(), b.end());
vector<string>::iterator c = unique(b.begin(), b.end());
b.erase(c, b.end());
stable_sort(b.begin(), b.end(), isShorter);
for(int i = 0; i < b.size(); ++i)
cout<< b[i] << ends;
cout << endl;
return 0;
}


算法十三:

对容器内容按照划分函数进行划分

#include<algorithm>

partition(起始迭代器,结束迭代器, 划分函数)

例子:

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
bool isLth5(const string &a)
{
return a.size() >= 5;
}
int main()
{
string a[] = { "12345", "ab", "cd", "12345"};
vector<string>b(a, a+4);
for(int i = 0; i < b.size(); ++i)
cout << b[i] << ends;
cout << endl;
vector<string>::iterator c = partition(b.begin(), b.end(), isLth5);
for(int j = 0; j < b.size(); ++j)
cout << b[j] << ends;
cout << endl;
b.erase(c, b.end());
for(int z = 0; z < b.size(); ++z)
cout << b[z] << ends;
cout << endl;
return 0;
}




























0 0
原创粉丝点击