Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3011504
  • 博文数量: 167
  • 博客积分: 613
  • 博客等级: 中士
  • 技术积分: 5473
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-13 21:35
个人简介

人, 既无虎狼之爪牙,亦无狮象之力量,却能擒狼缚虎,驯狮猎象,无他,唯智慧耳。

文章分类
文章存档

2015年(19)

2014年(70)

2013年(54)

2012年(14)

2011年(10)

分类: C/C++

2013-07-23 10:16:31

      为了简化程序的开发,C++在基本库的基础上又提供了两种对象库,一种是string库,专门用于处理可变长的字符串对象;另一种是vector,作为一个容器可以容纳其他的对象。上述两种库对象都是连续存放的内存模式。
关于string:
1. 使用string库时当然要加入头文件:#include ,当然命名空间要声明为std;
2. 初始化方式:string s1; //默认构造函数,s1为空串
                      string s1("Hello"); 
                      string s2(s1);
                      string s1(n, 'c'); //将s1初始化为'c'的n个副本
3. s1.size()函数返回s1中的字符个数;s1.empty()用来确认字符串是否为空;
4. 读入字符串:一种是直接从标准输入读入:cin >> s1; //以空白字符(空格、换行、制表符)为分界,从第一个非空白字符读入,再遇空白字符                                                                                  //终止
                     另一种则是按行读入:getline(cin, s1);   //仅仅不读换行符,并且以换行符为分界,开头的空格也会读入
5. 为了避免出错,使用string类类型定义的配套类型string::size_type来表示涉及元素个数的变量和处理
6. string类对象可以拼接,类似于python,但是要求"+"两边必须至少有一个是string类对象
string类库的基本操作:
对象初始化;
基本属性:size和empty;
操作:添加新元素和访问成员
特有操作:对象拼接

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. int main()
  5. {
  6.     string str("Hello\n"); //字面值常量初始化法将'\n'作为一个换行符计算
  7.     string::size_type size; //涉及到string长度/下标的变量必须使用string::size_type类型
  8.     cout << "str's size is :"
  9.          << (size = str.size())<< endl;
  10.     cout << "str is :"
  11.          <<str;
  12.     cout <<"Initial test End..."<< endl;
  13.     cout <<endl<<endl<<endl;
  14.     cout <<"A new test..."<< endl;
  15.     
  16.     cout <<"Please input three strings:..."<<endl;
  17.     string str1, str2, str3;
  18.     cin >> str1;
  19.     cin >> str2;
  20.     cin >> str3;
  21.     cout << "str1 + str2+ str3 :"
  22.          <<(str = str1 + str2 +str3)<<endl;
  23.     cout << "Now let's format the string..."<<endl;
  24.     cout <<str1 + " " + str2 + " " + str3 //string类型拼接符"+"必须与一个string类型相邻
  25.          <<endl; //string::"+"返回一个string类型对象所以 string + " " + "haha" is OK too
  26.     //cout << "hello" + " "; 这样写会报错,"+"两侧没有string类型对象
  27.     //即:string::"+"两侧不能都是字符串常量!
  28.     system("pause");
  29.     return 0;
  30. }
关于vector
1. vector是一个类模板,必须使用<>明确类型才成为一个具体的类类型,初始化同样有四种方式:
vector  v1;          //默认构造函数v1为空
vector v2(v1);
vector v3(n, i);     //包含n个值为i的元素
vetor  v4(n);    //包含有值初始化元素的n个副本
2. vector的属性操作:vector.size()和vector.empty(),当然长度/个数是vector对应的配套类型:vector::size_type
3. 访问vector元素可以借助于下标(同string),但是为了与其他容器一致起来(并非所有容器都可以使用下标访问成员),我们建议使用迭代器来访问成员。定义
vector vec
vector iterator iter = vec.begin();    //初始化一个迭代器指向vec的第一个元素
//当vector为空时,vec.begin() == vec.end()
*iter == vec[0];         //可以使用*iter来访问成员,iter++即可向后访问
4. vector对象只能用push_back()在后端添加成员,“=”仅仅用来为元素赋值

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <vector>

  3. using namespace std;

  4. int main()
  5. {
  6.     int num;
  7.     vector<int> vec;
  8.     while(cin >> num)
  9.     {
  10.         vec.push_back(num); //必须使用push_back()动态添加新元素
  11.               }
  12.     vector<int>::size_type size = vec.size();
  13.     cout << "vector's length is :" << size<<endl;
  14.     int sum = 0;
  15.     //试用下标来访问vector中元素
  16.     for (vector<int>::size_type index = 0; index != size; index++)
  17.     {
  18.         cout << "The " <<index <<" element is " << vec[index]<<endl;
  19.         sum += vec[index];
  20.     }
  21.     cout << "The sum of integer is: "<< sum<<endl;
  22.     //迭代器版本
  23.     sum = 0;
  24.     vector<int>::iterator iter = vec.begin();
  25.     for (vector<int>::size_type index = 0; iter != vec.end(); iter++)
  26.     {
  27.         cout <<" The"<<index <<" element is "<< *iter<<endl;
  28.         sum += *iter;
  29.     }
  30.     cout << "The sum of integer is: "<< sum<<endl;
  31.     
  32.     system("pause");
  33.     return 0;
  34.     
  35. }


阅读(6414) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~