zoukankan      html  css  js  c++  java
  • cb07a_c++_迭代器和迭代器的范围

    cb07a_c++_迭代器和迭代器的范围
    c++primer第4版
    https://www.cnblogs.com/txwtech/p/12309989.html
    --每一种容器都有自己的迭代器
    --所有的迭代器接口都是一样的
    --在整个标准库中,经常使用形参作为一对迭代器的构造函数
    --常用的迭代器操作
    *iter,++iter,--iter,iter1=iter2,iter1!=iter2
    --vector和deque容器的迭代器的额外操作,数组操作。可如下操作:
    inter+n,iter-n,>,>=,<,<= (vector和deque容器可操作这些符号)

    /////txwtech///

    --迭代器的范围
    begin/end,first/last
    --使迭代器失效的容器操作
    关联容器:set/map

    https://blog.csdn.net/txwtech/article/details/104371051

    https://www.cnblogs.com/txwtech/p/12325209.html

    习题:9.12
    编写一个函数,其形参是一对迭代器和一个 int 型数值,
    实现在迭代器标记的范围内寻找查找该 int 型数值的功能,
    并返回一个bool结果,以指明是否找到指定数据
    习题:9.13
    重写程序,查找元素的值,
    并返回指向找到的元素的迭代器。确保程序在要寻找的
    元素不存在时也能正确工作。
    习题:9.14
    使用迭代器编写程序,从标准输入设备读入若干 string 对象,并将它们存储在一 个 vector
    对象中,然后输出该 vector 对象中的所有元素
    习题:9.15
    用 list 容器类型重写习题 9.14 得到的程序,列出改变了容器类型后要做的修改

     1 /*cb07a_c++_迭代器和迭代器的范围
     2 c++primer第4版
     3 https://www.cnblogs.com/txwtech/p/12309989.html
     4 --每一种容器都有自己的迭代器
     5 --所有的迭代器接口都是一样的
     6 --在整个标准库中,经常使用形参作为一对迭代器的构造函数
     7 --常用的迭代器操作
     8      *iter,++iter,--iter,iter1=iter2,iter1!=iter2
     9 --vector和deque容器的迭代器的额外操作,数组操作。可如下操作:
    10 inter+n,iter-n,>,>=,<,<=  (vector和deque容器可操作这些符号)
    11 
    12 --迭代器的范围
    13    begin/end,first/last
    14 --使迭代器失效的容器操作
    15 关联容器:set/map
    16 
    17 习题:9.12
    18 编写一个函数,其形参是一对迭代器和一个 int 型数值,
    19 实现在迭代器标记的范围内寻找查找该 int 型数值的功能,
    20 并返回一个bool结果,以指明是否找到指定数据
    21 习题:9.13
    22 重写程序,查找元素的值,
    23 并返回指向找到的元素的迭代器。确保程序在要寻找的 
    24 元素不存在时也能正确工作。
    25 习题:9.14
    26 使用迭代器编写程序,从标准输入设备读入若干 string 对象,并将它们存储在一 个 vector
    27 对象中,然后输出该 vector 对象中的所有元素
    28 习题:9.15
    29 用 list 容器类型重写习题 9.14 得到的程序,列出改变了容器类型后要做的修改
    30 
    31 */
    32 #include <iostream>
    33 #include <deque>
    34 #include <vector>
    35 #include <list>
    36 
    37 using namespace std;
    38 
    39 int main()
    40 {
    41     vector<int> a;
    42     deque<int> b;
    43     list<int> c;
    44     a.push_back(1);
    45     a.push_back(2);
    46     a.push_back(3);
    47     a.push_back(4);
    48     a.push_back(5);
    49     vector<int>::iterator iter1 = a.begin();//指向a的一个数据
    50     vector<int>::iterator inter2 = a.end();//指向最后一个的下一个。
    51     cout << *iter1 << endl;
    52     iter1++;
    53     cout << *iter1 << endl;
    54     iter1--;
    55     cout << *iter1 << endl << endl;
    56 
    57     vector<int>::iterator first = a.begin();
    58     vector<int>::iterator last = a.end();
    59     while (first != last)
    60     {
    61         cout << *first << endl;
    62         first++;
    63     }
    64     cout << endl;
    65     vector<int>::iterator x = a.begin();
    66     vector<int>::iterator m = x + a.size() / 2;
    67     cout << "中间: " << *m << endl;
    68 
    69 
    70     return 0;
    71 }
     1 /*习题9.12
     2 习题:9.12
     3 编写一个函数,其形参是一对迭代器和一个 int 型数值,
     4 实现在迭代器标记的范围内寻找查找该 int 型数值的功能,
     5 并返回一个bool结果,以指明是否找到指定数据
     6 习题:9.13
     7 重写程序,查找元素的值,
     8 并返回指向找到的元素的迭代器。确保程序在要寻找的
     9 元素不存在时也能正确工作。
    10 习题:9.14
    11 使用迭代器编写程序,从标准输入设备读入若干 string 对象,并将它们存储在一 个 vector
    12 对象中,然后输出该 vector 对象中的所有元素
    13 习题:9.15
    14 用 list 容器类型重写习题 9.14 得到的程序,列出改变了容器类型后要做的修改
    15 */
    16 #include <iostream>
    17 #include <vector>
    18 
    19 using namespace std;
    20 
    21 bool findInt(vector<int>::iterator beg, 
    22              vector<int>::iterator end, int ival);
    23 
    24 int main()
    25 {
    26     vector<int> a;
    27     a.push_back(2);
    28     a.push_back(12);
    29     a.push_back(9);
    30     a.push_back(28);
    31 
    32     vector<int>::iterator k1 = a.begin();
    33     vector<int>::iterator k2 = a.end();
    34     k1++;
    35     k2--;
    36 
    37     //在k1与k2之间查找9
    38     bool result = findInt(k1, k2, 9);
    39     
    40     //在a.begin()与a.end()之间查找28
    41     //bool result = findInt(a.begin(),a.end(),28);
    42     if (true == result)
    43         cout << "find value" << endl;
    44     else
    45         cout << "not find " << endl;
    46 
    47     return 0;
    48 } 
    49 //                前包括,后不包括。包括beg,不包括end
    50 bool findInt(vector<int>::iterator beg,
    51     vector<int>::iterator end, int ival)
    52 {
    53     while (beg != end)
    54     {
    55         if (*beg == ival)
    56             break;
    57         else
    58             ++beg;
    59     }
    60     if (beg != end)
    61         return true;
    62     else
    63         return false;
    64 }
     1 /*习题9.13
     2 
     3 习题:9.13
     4 重写程序,查找元素的值,
     5 并返回指向找到的元素的迭代器。确保程序在要寻找的
     6 元素不存在时也能正确工作。
     7 
     8 */
     9 #include <iostream>
    10 #include <vector>
    11 
    12 using namespace std;
    13 
    14 vector<int>::iterator findInt(vector<int>::iterator beg,
    15     vector<int>::iterator end, int ival);
    16 
    17 int main()
    18 {
    19     int ia[] = {0,1,2,3,4,5,6};
    20     vector<int> ivec(ia,ia+7);//数组的名称就是指针,所ia就是指针、
    21     //数组赋值给ivec向量。
    22 
    23     vector<int>::iterator result= findInt(ivec.begin(),ivec.end(),5);
    24     if (result == ivec.end())
    25         cout << "没有找到!" << endl;
    26     else
    27         cout << "找到了" << endl;
    28 
    29 
    30     return 0;
    31 }
    32 //                前包括,后不包括。包括beg,不包括end
    33 vector<int>::iterator findInt(vector<int>::iterator beg,
    34     vector<int>::iterator end, int ival)
    35 {
    36     while (beg != end)
    37     {
    38         if (*beg == ival)
    39             break;
    40         else
    41             ++beg;
    42     }
    43     return beg;
    44 }
     1 /*
     2 
     3 习题:9.14
     4 使用迭代器编写程序,从标准输入设备读入若干 string 对象,并将它们存储在一 个 vector
     5 对象中,然后输出该 vector 对象中的所有元素
     6 习题:9.15
     7 用 list 容器类型重写习题 9.14 得到的程序,列出改变了容器类型后要做的修改
     8 */
     9 
    10 #include <iostream>
    11 #include <vector>
    12 #include <string>
    13 using namespace std;
    14 
    15 int main()
    16 {
    17     vector<string> svec;
    18     string str;
    19     cout << "Enter some trings(ctrl+Z to end):" << endl;
    20 
    21     while (cin >> str)
    22         svec.push_back(str);
    23     for (vector<string>::iterator iter = svec.begin();
    24         iter != svec.end(); ++iter)
    25         cout << *iter << endl;
    26     return 0;
    27 }
     1 /*
     2 
     3 
     4 习题:9.15
     5 用 list 容器类型重写习题 9.14 得到的程序,列出改变了容器类型后要做的修改
     6 */
     7 
     8 #include <iostream>
     9 #include <vector>
    10 #include <string>
    11 #include <list>
    12 using namespace std;
    13 
    14 int main()
    15 {
    16     //vector<string> svec;
    17     list<string> slst;
    18     string str;
    19     cout << "使用lis容器操作数据" << endl;
    20     cout << "Enter some trings(ctrl+Z to end):" << endl;
    21 
    22     while (cin >> str)
    23         slst.push_back(str);
    24     for (list<string>::iterator iter = slst.begin();
    25         iter != slst.end(); ++iter)
    26         cout << *iter << endl;
    27     return 0;
    28 }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    「UVA12293」 Box Game
    「CF803C」 Maximal GCD
    「CF525D」Arthur and Walls
    「CF442C」 Artem and Array
    LeetCode lcci 16.03 交点
    LeetCode 1305 两棵二叉搜索树中的所有元素
    LeetCode 1040 移动石子直到连续 II
    LeetCode 664 奇怪的打印机
    iOS UIPageViewController系统方法崩溃修复
    LeetCode 334 递增的三元子序列
  • 原文地址:https://www.cnblogs.com/txwtech/p/12310953.html
Copyright © 2011-2022 走看看