C++用户定义类型string和vector

1.string

1 #include <iostream>
  2 #include <string>
  3 using namespace std ;
  4 
  5 int main()
  6 {   
  7     string s = "hello", s2("world");
  8     cout << s.size() << endl; //输出字符串的长度
  9     string s3 = s.substr(1,3);//新定义一个s3,为字符串s下标1到下标3
 10     cout << s2 << endl;
 11     cout << s3 << endl; 
 12     string s4 = s + " " + s2;
  13     cout << s4 << endl;
 14     
 15     s4[0] = 'H';
 16     s4[6] = 'x';//重定义s4第0和第6个字符
 17     cout << s4 << endl;
 18     int pos = s4.find("orl");
 19     cout << pos <<endl;//找出orl位置
 20     s4.insert(3, "ABCDE");
 21     cout << s4 << endl;
 22 
 23     for(int i = 0; i < s4.size(); i++)
 24     {
 25         cout << s4[i] << "-";
 26     }
 27     cout << "\n";
 28 }

在这里插入图片描述

2.vector

  1 #include <iostream>
  2 #include <string>
  3 #include <vector>
  4 using namespace std;
  5 
  6 int main()
  7 {
  8     vector<int>v;
  9     //push_back(),最后面添加一个元素
 10     v.push_back(25);
 11     v.push_back(36);
 12     v.push_back(3);
 13     v.push_back(5);
 14     v.push_back(6);
 15     v.push_back(7);
 16     for(int i = 0; i < v.size(); i++)
 17     {
 18         cout << v[i] << '\t';
 19     }
 20     cout << '\n';
 21     v.resize(2);
 22     v.pop_back();
 23     for(int i = 0; i < v.size(); i++)
 24     {
 25         cout << v[i] << '\t';
 26     }
 27     cout << '\n';
 28     return 0;
 29 }

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值