C++基础::string

string 与 stl容器

归根结底,两者位于不同的库中,遵循不同的接口规范。

  • substr(first, nums)
  • find
  • find_first_of
  • find_first_not_of
  • find_last_of
  • find_last_not_of

注意后面四个string的成员函数都有一个of,因此判断的是字符(char)是否在传递进来的字符串,如下例的size_t first = str.find_first_of('abcdefghi..');

string类型的find函数返回的是下标也即整型值,STL容器无论是自身的find成员函数(set)还是和<algorithm>搭配的find全局函数返回的iterator

std::string str("hello world!");
size_t pos = str.find(...);
if (pos == std::string::npos)
    ...

std::vector<int> coll;
...
std::vector<int>::iterator pos = std::find(coll.begin(), coll.end(), 5);
if (pos == coll.end())
    ...

std::string来源于一个非STL的库,也因此它遵循另外一套接口规范,也就造成了一些容易混淆的情况:
比如,string::substr()方法,它也是两个参数(第一个参数默认为0,第二个参数默认为std::string::npos),但和STL惯例的接口不同(STL的参数常常用来标识一个左闭右开的区间),substr的第二个参数表示的是子串中元素的个数

我们删除一个字符串开头和结尾中的非字符为例,演示substr的用法:

std::string clean_str(const std::string& str)
{
    const std::string alphabet = "abcdefghijklmnopqrstuvwxyz1234567890";
    size_t first = str.find_first_of(alphabet);
    size_t last = str.find_last_of(alphabet);
    return str.substr(first, last-first+1);
    // 注意这里,而不是STL的做法,也即str.substr(first, last+1);
}

int main(int, char**)
{
    std::string str = "         test 123-4    ";
    std::cout << clean_str(str) << std::endl;
            // test 123-4
    return 0;
}

再看一个实例:

std::string filename = "hello.txt", basename, extname;
auto idx = filename.find('.');
if (idx != std::string::npos)
{
    basename = filename.substr(0, idx);
    extname = filename.substr(idx+1);
}

一个应用实例

如果文本格式是:用户名 电话号码,

// name.txt 
Tom 23245332 
Jenny 22231231 
Heny 22183942 
Tom 23245332
...

现在我们需要对用户名排序,且只输出不同的姓名。
我们可以想见的思路(C++做法):读文件,文件打开成功与否的判断,解析保存到容器,排序,去重(注意如果使用stl中的unique算法的话,排序在前,去重在后,这一点不懂的可翻看unique算法工作原理)。典型地流程化的处理方式,我们只需要根据步骤step by step便可轻松解决问题:

#include <fstream>
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

int main(int, char**)
{
    std::ifstream in("./names.txt");
    assert(in.good() == true);
    std::string strtmp;
    std::vector<std::string> names;
    while(std::getline(in, strtmp, '\n'))
        names.push_back(strcmp.substr(0, strcmp.find(" ")));        
    std::sort(strcmp.begin(), strcmp.end());
    std::unique(strcmp.begin(), strcmp.end());
    std::copy(strcmp.begin(), strcmp.end(), std::ostream_iterator<std::string>(std::cout, " "));
    std::cout << std::endl;
    return 0;
}

当然我们可充分利用set容器的性质(没有重复且自动排序,可在构造时,指定排序规则,默认为升序即:std::less<std::string>)

#include <set>
#include <functional>           // for std::greater<std::string>
int main(int, cha**)
{
    std::ifstream in("./names.txt");
    std::string strtmp;
    std::set<std::string, std::greater<std::string>> names;
    while (std::getline(in, strtmp, '\n'))
        names.insert(strtmp.substr(0, strtmp.find(" ")));
                                // 集合的性质,自动完成去重和排序
    std::copy(names.begin(), names.end(), std::ostream_iterator<std::string>(std::cout, " "));

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值