。
1) STL 基础
stl 算法知识
Using the STL generic reverse algorithm with an array
Use random_shuffle algorithms with array
include
#include
#include
#include // For reverse algorithm
using namespace std;
int main()
{
char array1[] = "abc";
int N1 = strlen(array1);
int a[100], b[100];
int i;
reverse(&array1[0], &array1[N1]);
for (i = 0; i < N1; i++)
cout << "array1[" << i << "] = " << array1[i] << endl;
for (i = 0; i < 100; ++i)
a[i] = i;
reverse_copy(&a[0], &a[100], &b[0]);
for (i = 0; i < 100; i++)
cout << "a: " << a[i] << " -- b: " << b[i] << endl;
random_shuffle(&a[0], &a[100]);
for (i = 0; i < 100; i++)
cout << " " << a[i] << " ";
return 0;
}
1.2 巧用 C++ 函数模板
Using function templates to get the max and min value in an array
include
using std::cout;
using std::endl;
template T max(const T* data, int size) {
T result = data[0];
for(int i = 1 ; i < size ; i++)
if(result < data[i])
result = data[i];
return result;
}
template T min(const T* data, int size) {
T result = data[0];
for(int i = 1 ; i < size ; i++)
if(result > data[i])
result = data[i];
return result;
}
int main() {
double data[] = {1.5, 4.6, 3.1, 1.1, 3.8, 2.1};
int numbers[] = {2, 22, 4, 6, 122, 12, 1, 45};
const int dataSize = sizeof data/sizeof data[0];
cout << "Minimum double is " << min(data, dataSize) << endl;
cout << "Maximum double is " << max(data, dataSize) << endl;
const int numbersSize = sizeof numbers/sizeof numbers[0];
cout << "Minimum integer is " << min(numbers, numbersSize) << endl;
cout << "Maximum integer is " << max(numbers, numbersSize) << endl;
return 0;
}
/*
Minimum double is 1.1
Maximum double is 4.6
Minimum integer is 1
Maximum integer is 122
*/
Use back_insert_iterator to insert element into a vector
原文链接: https://www.cnblogs.com/shixinzhu/archive/2012/03/05/2380203.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/43288
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!