C++ string 使用指南(字符串处理必备)

C++ string 使用指南

字符串输入

  • 单词输入,遇见空白停止,但\n仍在输入流
cin >> str;
  • 整行输入,遇见\n停止并丢弃\n,也可指定其他停止符
getline(cin, str); //常规输入

bool getline(istream & is, string  & str, char delim); //函数原型(可定制输入 )
  • 单字符输入
cin.get(ch) //方式1
ch = cin.get() //方式2

字符放回数据流

  • 上一个读取的字符放回输入流
cin.unget();
istream& unget(); //函数原型
  • 指定字符放回输入流
cin.pushback(ch);
istream& putback (char c); //函数原型

常用库函数

#include <string>

  • 成员函数,用于获取子串(start, len),注意第二个参数为子串最大长度
string substr(int star=0, int len=string::npos);
  • 成员函数,用于从位置start向后查找字符串target,返回目标首字符索引,找不到时返回值为string::npos
int find(string target, int start); 
// rfind 、find_first_of 、find_first_not_of、find_last_of、find_last_not_of
  • 成员函数,用于把指定子串(start, le)替换成另一 字符串(dest)原地操作并返回自身引用
string & replace(int start, int len, string dest);
  • 成员函数,用于删除子串(start, len)
string & erase(int start, int len);

字符串分割函数split

  • 利用字符串流stringstream可以超级简单地实现
#include <iostream>

#include <string>
#include <sstream>
#include <vector>

using namespace std;

vector<string> split(string text, char delim)
{
    stringstream ss(text);
    string item;
    vector<string> word_block;
    while(getline(ss, item, delim))
        word_block.push_back(item);
    return word_block;
}

int main()
{
    string text;
    while(getline(cin, text))
    {
        vector<string> wb = split(text, ' ');
        for(string w: wb)
            cout << w << " ";
        cout << "\n";
    }
    return 0;
}

单词替换

  • 思路一: 查找替换法,需要注意单词与字符串的区别
  • 思路二: 分词替换法,可以借用split方法方便地实现
string word_replace(string text, const string & src, const string & dest)
{
    int pos = 0;
    while((pos=text.find(src, pos)) != string::npos)
    {
        int end_pos = pos + src.size();
        if(end_pos == text.size() || text[end_pos] == ' ')
            text.replace(pos, src.size(), dest);
        pos = text.find(" ", pos);
    }
    return text;
}

常用字符函数

#include <cctype>

bool isdigit(char ch); //0-9
bool isalpha(char ch); //(a-z)(A-Z)
bool isspace(char ch); // [\t \r\n]
bool islower(char ch); //a-z
bool isupper(char ch); //A-Z

char tolower(char ch);
char toupper(char);

正则表达式

#inlude <regex>

  • regex-pattern的构造
string pat_str = "\\w+@\\w"; //a mail pattern
regex pattern1(pat_str);
regex pattern2(pat_str, regex::icase) // 匹配时忽略字母大小写
  • 正则匹配
bool regex_match(string str, regex pattern);//仅用于判别匹配
bool regex_match(string str, smatch match, regex pattern); //用于获取匹配子串
  • 正则替换, replace_str中可包含通过转义方式特定串进行引用
string & regex_match(string & str, regex, string replace_str);
string origin_text = "I have a email with address of " + "ant_xinyuan@163.com" + ".\n";
regex pattern("(\\w+)@(\\w+)\\.com");

string new_text = regex_replace(big_text, pattern, "SECERT");
// new_text: I have a email with address of " + "ant_xinyuan@163.com" + ".\n";
转义符含义举例备注
$&整个匹配部分ant_xinyuan@163.commatch[0]
$n第n个匹配子部分ant_xinyuanmatch[1]
$`匹配的前缀I have a email with address of
$’匹配的后缀.\n
$$$符号本身$
  • 正则匹配子串获取
#include <iostream>
#include <string>
#include <regex>
using namespace std;

int main()
{
    string text = "ant_xinyuan@163.com";
    string pat_str = "(\\w+)@(\\w+)\\.com";
    regex pattern(pat_str);
    smatch match;
    if(regex_match(text, match, pattern))
    {
        for(int i = 0; i < match.size(); i++)
            cout << i << ": " << match[i] << "\n";
    }
    else
        cout << "Not match!\n";
    return 0;
}

/* output
0: ant_xinyuan@163.com
1: ant_xinyuan
2: 163
*/

字符串数字转化

#include <sting>

  • 数字转字符串
string to_string(int|long|float|double|...| digit);
  • 字符串转数字(任意进制)
int stoi(string str, size_t* p=nullptr, int base=10);
long stol(string str, size_t* p=nullptr, int base=10);
float stof(string str, size_t* p=nullptr, int base=10);
double stod(string str, size_t* p=nullptr, int base=10);
long double stold(string str, size_t* p=nullptr, int base=10);
...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值