C++之vector和迭代器

vector的简单使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <vector>
#include <string>
#include <iostream>
using  std::vector;
using  std::string;
using  std::cout;
using  std::endl;
int  main(){
//  vector是一个类模板
//  定义和初始化vector对象
     vector<string> s_v;  //默认初始化,不含有任何元素
 
     vector< int > ivec;
     //两个vector对象的元素类型必须相同
     vector< int > ivec2(ivec);  //把ivec的元素拷贝给ivec2,副本
     vector< int > ivec3 = ivec;   //把ivec的元素拷贝给ivec2,副本
 
     //列表初始化vector对象,c++11使用的语法
     vector<string> names = { "kyx" , "sdsd" , "sdsdsd" };
     vector<string> persons{ "aaa" , "bbb" , "ccc" };
 
     //创建指定数量的元素
     vector<string> v1(10, "ssss" );  //10个string对象,每个初始化为ssss
     vector<string> v7{10, "a" };   //10个string对象
 
 
     //值初始化
     vector< int > intvec(10);  //10个元素,每个初始化为0
     vector<string> sv2(10);  //10个元素,每个元素初始化为空字符串
 
     vector<string> v5{ "hi" };   //列表初始化:只有一个元素
     vector<string> v6{10};    //10个执行了默认初始化的元素
 
     for ( auto  i:v7){
         cout<<i<< "\n" <<endl;
     }
 
 
     //向vector对象中添加元素,使用push_back成员函数
     vector< int > v9 ;
     for ( int  i=0;i!=100;i++){
         v9.push_back(i);
     }
 
     for ( auto  i:v9){
         cout<<i<< "\n" <<endl;
     }
 
     //vector.size()成员函数返回类型为size_type
     vector< int >::size_type num = intvec.size();
     cout<< "intvec容器包含元素的个数=" <<num<<endl;
 
     //vector内对象的索引和string一样,也是从0开始记起
     //可以直接使用下标获取索引位置上的元素
     //但不能用下标形式添加元素
     return  0;
}

迭代器iterator

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <vector>
#include <string>
#include <iostream>
using  std::vector;
using  std::string;
using  std::cout;
using  std::endl;
int  main(){
     //迭代器
     /*
     v.begin();返回执行第一个元素的迭代器
     v.end();返回返回指向容器尾元素的下一个位置(one past the end)
     end返回的迭代器常被称为尾后迭代器
     如果容器为空,则begin和end返回的是同一个迭代器
     */
 
     string s= "do some thing" ;
     if (s.begin()!=s.end()){   //确保s非空
         auto  it = s.begin();
         *it =  toupper (*it);   //将字符改成大写形式
     }
     cout<<s<< " " <<endl;
 
     //将迭代器从一个元素移动到另外一个元素,遇到空白字符结束
     for ( auto  it = s.begin();it!=s.end()&&! isspace (*it);++it){
         *it=  toupper (*it);
     }
     cout<<s<< "" <<endl;
 
     //迭代器类型
     vector< int >::iterator it ;  //it只能读写vector<int>中的元素
     string::iterator str_it ;  //str_it只能读写string对象中的字符
 
     //如果对象时常量,begin和end返回const_iterator
     //如果对象不是常量,begin和end返回iterator;
     vector< int >::const_iterator it2 ;  //it2只能读元素,不能写元素
     string::const_iterator it3 ;  //it3只能读字符,不能写字符
 
     //c++11引入的新函数cbegin和cend
     vector< int > abc = {1,3,4,556,67};
     vector< int >::const_iterator abc_begin = abc.cbegin();
     vector< int >::const_iterator abc_end = abc.cend();
     if (abc_begin!=abc_end){
         cout<< "abc容器不是空的!!!!!!" <<endl;
     }
 
     /*
     解引用和成员访问操作符-*-.
     箭头运算符 ->
     */
     for ( auto  it = abc.begin();it!=abc.end();it++){
         if ((*it)>10){
             cout<< "该元素大于10!!!" <<endl;
         }
     }
 
     for ( auto  it = s.begin();it!=s.end();it++){
         if ( isspace (*it)){
             cout<< "该字符是空的!!!" <<endl;
         }
     }
 
     vector<string> strv = { "ab" , "\0" , "" , "gh" };
     for ( auto  it = strv.begin();it!=strv.end();it++){
         if (it->empty()){
             cout<< "该字符串是空的!!!" <<endl;
         }
     }
 
     /*
     迭代器的算术运算
     可以执行加减和大于小于不等于等操作
     */
     return  0;
}

=====END=====




FROM: http://my.oschina.net/xinxingegeya/blog/225474

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值