📄 unitmain.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <iostream.h>
#include <stdlib.h> // Need random(), srandom()
#include <time.h> // Need time()
// STL算法头文件(注意没有.h后缀)
#include <algorithm> // Need sort(), copy()
#include <vector> // Need vector
using namespace std;
#pragma hdrstop
#define SIZE 100
int iarray[SIZE];
vector<int> intVector(100);
double darray[10] = {1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9};
vector<double> vdouble(10);
//---------------------------------------------------------------------------
void Display(vector<int>& v, const char* s);
#pragma argsused
int main(int argc, char* argv[])
{
// =================== 指针迭代器 =========================
cout << "指针迭代器" << endl;
iarray[20] = 50;
int* ip = find(iarray, iarray + SIZE, 50);
if (ip == iarray + SIZE){ // 如果表达式为真,则表示在搜索的范围内没有指定的值
// 当使用STL函数时,只能测试ip是否和past-the-end 值是否相等
// 而测试函数返回值和NULL是否相等是不正确的[if (ip != NULL)]
cout << "50 not found in array" << endl;
}else{
cout << *ip << " found in array" << endl;
}
/***********************************************************************************************
* find()函数接受三个参数。头两个定义了搜索的范围。 *
* 由于C和C++数组等同于指针, 表达式iarray指向数组的第一个元素。 *
* 而第二个参数iarray + SIZE等同于past-the-end 值,也就是数组中最后一个元素的后面位置。 *
* 第三个参数是待定位的值,也就是50。 *
* find()函数返回和前两个参数相同类型的迭代器,这儿是一个指向整数的指针ip。 *
***********************************************************************************************/
// =================== 常量迭代器 =========================
vector<int>::iterator first; // 创建了一个vector<int>类的迭代器
// 将该迭代器设置到intVector的第一个对象,并将它指向的对象值设置为123
first = intVector.begin();
*first = 123;
// =================== 容器迭代器 =========================
cout << " \n容器迭代器" << endl;
intVector[20] = 50;
vector<int>::iterator intIter = find(intVector.begin(), intVector.end(), 50);
if (intIter != intVector.end())
cout << "Vector contains value " << *intIter << endl; // 显示搜索到的数据
else
cout << "Vector does not contain 50" << endl;
// =================== 输出迭代器 =========================
cout << " \n输出迭代器" << endl;
vector<double>::iterator outputIterator = vdouble.begin();
copy(darray, darray + 10, outputIterator); // 当使用copy()算法的时候,必须确保目标容器有足够大的空间,或者容器本身是自动扩展的
while (outputIterator != vdouble.end()) {
cout << *outputIterator << endl;
outputIterator++;
}
// ========================= 前推迭代器 =============================
// 前推迭代器能够读写数据值,并能够向前推进到下一个值。但是没法递减。
//===================================================================
cout << " \n前推迭代器" << endl;
replace(vdouble.begin(), vdouble.end(), 1.5, 3.14159); // replace()将[first,last]范围内的所有值为old_value的对象替换为new_value。:
outputIterator = vdouble.begin();
while (outputIterator != vdouble.end()) {
cout << *outputIterator << endl;
outputIterator++;
}
// =================== 双向迭代器(双向迭代器要求能够增减) =========================
cout << " \n双向迭代器" << endl;
reverse(vdouble.begin(), vdouble.end()); // reverse() 函数来对容器进行逆向排序:
outputIterator = vdouble.begin();
while (outputIterator != vdouble.end()) {
cout << *outputIterator << endl;
outputIterator++;
}
// =================== 随机访问迭代器 =========================
// 以任意顺序访问数据,并能用于读写数据(不是const的C++指针也是随机访问迭代器)。
// STL的排序和搜索函数使用随机访问迭代器。随机访问迭代器可以使用关系操作符作比较。
// ============================================================
cout << " \n随机访问迭代器" << endl;
random_shuffle(vdouble.begin(), vdouble.end()); // random_shuffle 随机打乱原先的顺序
outputIterator = vdouble.begin();
while (outputIterator != vdouble.end()) {
cout << *outputIterator << endl;
outputIterator++;
}
//================= 迭代器技术:流和迭代器 ==================================
// Seed the random number generator
random( time(NULL) );
// Construct vector and fill with random integer values
vector<int> collection(10);
for (int i = 0; i < 10; i++)
collection[i] = rand() % 10000;;
// Display, sort, and redisplay
Display(collection, "Before sorting");
sort(collection.begin(), collection.end());
Display(collection, "After sorting");
getchar();
return 0;
}
//---------------------------------------------------------------------------
// Display label s and contents of integer vector v
void Display(vector<int>& v, const char* s)
{
cout << endl << s << endl;
// copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\t")); //暂时没有搞定ostream_iterator
cout << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -