C++必备(一)string类的类型转换和基本操作

自存备忘

string 字符串

//首先需要引入头文件
#include <string>  //DEV C++好像不引也没问题

1. 创建

string s1;
string s2(s1); //等价于string s2 = s1; 深拷贝,s1的改变不影响s2
string s3("value");//等价于 string s3 = "value"
string s4(7,'c')  ;//s4是ccccccc

2. 基本操作

2.1 字符串输入输出
string s;
// 输入字符串
cin >> s1;  //cin从第一个非空格非tab字符开始读,读到下一个空格或tab字符(不包括)结束
getline(cin,s);//从输入流缓冲区第一个字符开始读到换行符结束
				//要注意此时缓冲区里可能残留的字符
//输入流可以是文件输入流等

2.2 常用函数及方法
//拼接
string s1 = s2 + "hahaha"; //要确保+两侧运算对象至少有一个是string
string s1 = s2 + "这是" + "可以的"

//访问元素
cout<< s1[0]; //下标访问
s1[0] = 'n';
s1.at(0);  //at()函数遍历,可以捕捉异常


s1.empty(); //返回是否为空(bool)
s1.length(); //返回字符串长度(int)
s1.compare(s2);//字符串比较

//字符串查找
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;
size_t find (char c, size_t pos = 0) const;
   //返回的都是位置下标

//插入
s1.insert(4,"new");  //在下标为4的地方插入new
s1.insert(4,s2); 
string& insert (size_t pos, size_t n, char c);
void insert (iterator p, InputIterator first, InputIterator last);//插入两个迭代器中间的一段
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);//插入另一个字符串的子串

//追加
append(); //参数和insert差不多

//取子串
string a = "20170816";
string b = a.substr(0,4); //起点,取的长度 b为2017
string c = a.substr(4);   //起点 c是0816


//字符串替换
basic_string& replace (size_type p0, size_type n0, const E * s); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换
basic_string& replace (size_type p0, size_type n0, const E *s, size_type n); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换 1 个字符
basic_string& replace (size_type p0, size_type n0, const basic_string& str); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换
basic_string& replace (size_type p0, size_type n0, const basic_string& str, size_type pos, size_type n); //使用串 str 的子串 str [pos, pos + n-1] 替换源串中的内容,从位置 p0 处开始替换,替换字符 n0 个
basic_string& replace (size_type p0, size_type n0, size_type n, E c); //使用 n 个字符 'c' 替换源串中位置 p0 处开始的 n0 个字符
basic_string& replace (iterator first0, iterator last0, const E * s);//使用迭代器替换,和 1) 用法类似
basic_string& replace (iterator first0, iterator last0, const E * s, size_type n);//和 2) 类似
basic_string& replace (iterator first0, iterator last0, const basic_string& str); //和 3) 类似
basic_string& replace (iterator first0, iterator last0, size_type n, E c); //和 5) 类似
basic_string& replace (iterator first0, iterator last0, const_iterator first, const_iterator last); //使用迭代器替换

3. 格式转换

3.1 char*、char[] 转换为 string

将 char*、char[] 转换为 string 类型时,直接进行赋值操作,将 char*、char[] 的变量赋值给 string 对象即可。
char* 类型可以直接用pringf函数的%s格式化输出

3.2 string 转换为 char*

使用c_str()函数或者data()函数

#include <string>
#include <iostream>
using namespace std;
int main()
{
    string str = "Hello World";
 
    // string 转换为 char*
    const char* cstring = str.c_str(); //注意const
    //等价于 const char* cstring = str.data();
    
    printf("[printf] cstring is: %s\n", cstring); 

    return 0;
}
 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值