龙空技术网

for和for_each操作C++的vector动态数组

福人的分享 140

前言:

现在你们对“for each数组”大致比较珍视,兄弟们都想要了解一些“for each数组”的相关内容。那么小编在网摘上收集了一些对于“for each数组””的相关知识,希望同学们能喜欢,朋友们一起来学习一下吧!

操作C++ 标准库STL的动态数组,回顾python和perl的数组列表,感觉它们各有特色都非常精彩,花在它们上的学习时间很过瘾。我是在DeepIN系统上用gcc/g++,分享我用for和for_each操作C++的vector动态数组习练代码。

字符串数组的for

这个for看上去非常像perl的列表数组 for (@msg),如果把:看成了in它又像是python列表数组。C++中那个引用的&word就是msg数组中的元素,加上for它就遍历了所有的元素并用cout显示。

    vector<string> msg = {"Fine!", "the C++", "World", "from", "Deepin", "with ATOM!"};    for (string &word : msg)    {        cout << word << " ";    }    cout << endl;
整数组成数组的for

与字符串类似,操作方法不变。push_back是向数组未必追加元素。

    vector<int> v = {1,2,3,4};    v.push_back(7); v.push_back(9);    for (int &mint : v) {        cout << mint << " " << endl;    }
字符组成数组的for

和整数、字符串操作类似。

    vector<char> v1 = {'a','b','c','d'};    v1.push_back('m'); v1.push_back('n');    for (char &mchar : v1) {        cout << mchar << " " << endl;    }
重点试练的字符串数组
    vector<string> v5 = {"aadf","sdfsf","werwr"};    v5.push_back("hello"); v5.push_back("world");    for (string &myvar : v5) {            //for (string myvar : v5)      cout << myvar << " " << endl;    }

这数组没什么特别的,for (string &myvar : v5) 也可以用 for (string myvar : v5),也能正常编译,应该还是引用或叫别名效率更高吧。

还可以这样for操作,auto知道itemsi的类型。

    for (auto itemsi = v5.begin(); itemsi != v5.end(); itemsi++)    {      printf("v5b: ", *itemsi);      cout << "v5b: " << *itemsi << endl;    }

还可以给它加上迭代器指针,用指针操作。

    vector<string>::iterator iters;    for (iters = v5.begin(); iters != v5.end(); iters++)    {      printf("v5a: ", *iters);      cout << "v5a: " << *iters << endl;    }
用for_each操作
    for_each(msg.begin(), msg.end(), funcs);    for_each(v5.begin(), v5.end(), funcs);    for_each(v.begin(), v.end(), funci);

funcs, funci分别是字符串和整数型函数,它们负责接收数组中的元素。比如:

void funcs(string elem){    cout << elem << endl;}void funci(int elem){    cout << elem*3 << endl;}

下面是完整的习练代码

#include<functional>#include<algorithm>#include <iostream>#include <vector>#include <string>#include <iterator>using namespace std;void funcs(string elem){    cout << elem << endl;}void funci(int elem){    cout << elem*3 << endl;}int main(){    vector<string> msg = {"Fine!", "the C++", "World", "from", "Deepin", "with ATOM!"};    for (string &word : msg)    {        cout << word << " ";    }    cout << endl;    vector<int> v = {1,2,3,4};    v.push_back(7); v.push_back(9);    for (int &mint : v) {        cout << mint << " " << endl;    }    vector<char> v1 = {'a','b','c','d'};    v1.push_back('m'); v1.push_back('n');    for (char &mchar : v1) {        cout << mchar << " " << endl;    }    vector<string> v5 = {"aadf","sdfsf","werwr"};    v5.push_back("hello"); v5.push_back("world");    for (string &myvar : v5) {            //for (string myvar : v5)      cout << myvar << " " << endl;    }    for (auto itemsi = v5.begin(); itemsi != v5.end(); itemsi++)    {      printf("v5b: ", *itemsi);      cout << "v5b: " << *itemsi << endl;    }    vector<string>::iterator iters;    for (iters = v5.begin(); iters != v5.end(); iters++)    {      printf("v5a: ", *iters);      cout << "v5a: " << *iters << endl;    }    for_each(msg.begin(), msg.end(), funcs);    for_each(v5.begin(), v5.end(), funcs);    for_each(v.begin(), v.end(), funci);    return 0;}

vector除了支持简单类型的数组外,也支持类作为元素,在for_each时把funcs或funci换成类中的函数,然后作为指针进行bind即可。我不知道C++依然这么优秀,很喜欢。继续学习,有高人路过请随时指导!

标签: #for each数组