第一章认识C++对象1.2.6泛型算法应用于普通数组
// 第一章认识C++对象1.2.6泛型算法应用于普通数组.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> /** * 要输出数组的内容,对数组进行升幂排序,反转数组的内容、复制数组的内容等操作 包含头文件 <algorithm> * 要对数组进项降幂排序和检索 需要包含头文件<functional> */ #include <algorithm> #include <functional> using namespace std; int main() { /* double a[] = { 1.1, 4.4, 3.3,2.2 }, b[4]; copy(a, a + 4, ostream_iterator<double>(cout, " ")); //正向输出数组a , 以空格隔开 cout << endl; reverse_copy(a, a + 4, ostream_iterator<double>(cout, " ")); // 逆向输出数组a, 以空格隔开 cout << endl; copy(a, a + 4, b); copy(b, b + 4, ostream_iterator<double>(cout, " ")); //正向输出数组b , 以空格隔开 cout << endl; sort(a, a + 4); copy(a, a + 4, ostream_iterator<double>(cout, " ")); //正向输出数组a , 以空格隔开 cout << endl; reverse_copy(a, a + 4, b); copy(b, b + 4, ostream_iterator<double>(cout, " ")); //正向输出数组b , 以空格隔开 cout << endl; */ //double a[] = { 1.1, 4.4, 3.3,2.2 }; //sort(a, a + 4); //copy(a, a + 4, ostream_iterator<double>(cout, " ")); //正向输出数组a , 以空格隔开 //cout << endl; //sort(a, a + 4, greater<double>()); //copy(a, a + 4, ostream_iterator<double>(cout, " ")); //正向输出数组a , 以空格隔开 //cout << endl; //double* x = find(a, a + 4, 4.4); //if (x == a + 4) cout << "没有值为4.4的数组的元素\n"; //else cout << "有值为" << *x <<"的数组的元素\n"; ///*cout << x << endl; //cout << a + 4 << endl; //*/ //x = find(a, a + 4, 8); //if (x == a + 4) cout << "没有值为8的数组的元素\n"; //else cout << "有值为" << *x << "的数组的元素\n"; //cout << x << endl; //cout << a + 4 << endl; // 演示对一维数组进行局部操作的例子 double a[] = { 1.1,4.4,3.3,2.2 }, b[8] = { 8 }; copy(a + 2, a + 4, ostream_iterator<double>(cout, " ")); // 输入a[2],a[3] cout << endl; reverse_copy(a + 1, a + 4, ostream_iterator<double>(cout, " ")); // 输入a[3],a[1] cout << endl; copy(a, a + 4, &b[4]);// 将数组复制到数组B的尾部 copy(b, b + 8, ostream_iterator<double>(cout, " ")); //正向输出数组b , 以空格隔开 cout << endl; sort(a + 1, a + 3); // 对部分数组元素升幂排序 copy(a, a + 4, ostream_iterator<double>(cout, " ")); //正向输出数组a , 以空格隔开 cout << endl; sort(b, b + 6, greater<double>()); copy(b, b + 8, ostream_iterator<double>(cout, " ")); //正向输出数组b , 以空格隔开 cout << endl; return 0; }