C++中string类的各种用法详解

本文整理自《ACM程序设计》(北京大学出版社)蓝皮书 曾棕根编著

1.首先先看一下reverse反向排序函数:

//@auther Yang Zongjun
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 10;
const int INF = 2100000000;



int main()
{
    //freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);
    vector<int> a(10);
    int n = 10;
    for(int i = 0; i< n; i++)<pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
/*关于头文件的一点小说明:
以前早期的C语言版本使用<string.h>头文件,
C中没有string类这一概念(string类是C++中仅有的,
因此C++中为string类封装了<string>这一个头文件,
其中包含了各种string类的相关运算,而这是C所没有的),
而C++中<cstring>是<string.h>的升级版本,
*/

using namespace std;

int main()
{
    //freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);
    string s;
    char ss[100];
    scanf("%s", ss);//ss前面不要&符号,因为ss本身就是地址
    s = ss;
    //printf("%s", s);// 这是错误做法,c的scanf和printf不能
                        //读取输出string对象,只能读或数字符数组
    cout << s << endl;
    return 0;
}

{ //a[i] = i; a[i] = i; } int i = 0; while(i < 10) { printf("%d ", a[i]); //cout << a[i] << " "; i++; } printf("\n"); reverse(a.begin(), a.end()); i = 0; while(i < 10) { printf("%d ", a[i]); i++; } /* cout << "---------------------->" << endl; int arr[10]; for(int i = 0; i < 10; i++) { arr[i] = i; cout << arr[i] << " "; } cout << endl; arr.reverse(arr, arr[9]); for(int i = 0; i < 10; i++) { cout << arr[i] << " " ; } cout << endl; */ return 0;}
 

如上可见:reverse可用于vector和下面的string类、set、map中,但需注意不能用于数组,因为数组没有这一成员函数。

2

<span style="font-size:10px;">#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
/*关于头文件的一点小说明:
以前早期的C语言版本使用<string.h>头文件,
C中没有string类这一概念(string类是C++中仅有的,
因此C++中为string类封装了<string>这一个头文件,
其中包含了各种string类的相关运算,而这是C所没有的),
而C++中<cstring>是<string.h>的升级版本,
*/

using namespace std;

int main()
{
    //freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);
    string s;
    char ss[100];
    scanf("%s", ss);//ss前面不要&符号,因为ss本身就是地址
    s = ss;
    //printf("%s", s);// 这是错误做法,c的scanf和printf不能
                        //读取输出string对象,只能读或数字符数组
    cout << s << endl;
    return 0;
}
</span>

3

<pre name="code" class="cpp">
 
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;
/*
再是用迭代器iterator时,如:string::iterator it = s.end()
则it指向s的最后一个元素的右边。
类似,it= sbegin()则指向s的第一个元素的左边。
*/

int main()
{
    //freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);
    string s = "123";
    //给string对象尾部添加元素只需用+号
    s += "456";cout << s << endl;s.clear();
    
    //insert()函数
    s = "123456";
    string::iterator ite = s.begin();
    //s.insert(it+1, "pdfedfsd");//错误,
    //insert函数一次只能插入一个字符
    s.insert(ite+1, 'p');
    cout << s << endl;
    
    //eraser()函数
    s = "abc123456";
    string::iterator iter = s.begin();
    //eraser函数可以删除指定的一个元素或者区间中的元素
    s.erase(iter+3);
    cout << s << endl;
    s.erase(iter, iter+4);
    cout <<s << endl;
    
    //replace()函数
    s = "abc123456";
    //从第三个开始,讲连续的三个字符替换为good""
    s.replace(3,3, "good");
    cout << s << endl;
    return 0;
}
/*
1.empty()判断是否为空
2.函数和length()、size()测长度
3.find()函数查找第一次出现的一个字符(单引号‘’定界)或一串字符(双引号”“定界),
    如果找到则返回下标值(从0开始计数),否则返回4294967295
以上函数不详细介绍了
*/


4

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

int main()
{
    //freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);
    
    //用reverse反向排序string对象
    string s = "123456789";
    reverse(s.begin(), s.end());
    cout << s << endl;
    
    //string对象作为vector元素
    vector<string> v;
    v.push_back("Jack");
    v.push_back("Mike");
    v.push_back("Tom");
    cout << v[0] << endl;
    cout << v[1] << endl;
    cout << v[2] << endl;
    cout << v[0][0] << endl;
    cout << v[1][0] << endl;
    cout << v[2].length() << endl;
    return 0;
}

5

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;
/*
C语言中,sscanf()函数很管用,他可以把一个字符串
按照你需要的方式分离出子串,甚至是数字, 下面演示了sscanf的用法
*/
int main()
{
    //freopen("C:/Users/Administrator/Desktop/input.txt", "r", stdin);
    char sa[100], sb[100], sc[100];
    //将字符串分离为子串,分隔符为空格
    sscanf("abc 123 pc", "%s %s %s", sa, sb, sc);
    cout << sa << " " << sb << " " << sc << " " << endl;
    //将字符串分离为数字,分隔符为空格
    int a, b, c;
    sscanf("1 2 3", "%d %d %d", &a, &b, &c);
    cout << a << b << c << endl;
    将字符串分离为数字,分隔符为","和"$"
    int x, y, z;
    sscanf("4,5$6", "%d,%d$%d", &x, &y, &z);
    cout << x << y << z << endl;
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值