C++/STL std::string 的用法

本文介绍了C++中处理字符串的多种实用技巧,包括大小写转换、去除空白字符、字符串替换及切割等操作,并展示了如何利用标准库函数实现高效的数据转换。

1.toupper, tolower

#include <iostream>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iterator>

#include <cctypes>

string s("heLLo");
transform(s.begin(), s.end(), s.begin(), toupper);
cout << s << endl;
transform(s.begin(), s.end(), s.begin(), tolower);
cout << s << endl;

 

2.trim
string s("   hello   ");
    s.erase(0, s.find_first_not_of(" /n"));
    cout << s << endl;
    s.erase(s.find_last_not_of('' '') + 1);
    cout << s << endl;

注意由于 find_first_not_of 和 find_last_not_of 都可以接受字符串,这个时候它们寻找该字符串中所有字符的 absence ,所以你可以一次 trim 掉多种字符。

 

 

3.erase

string 本身的 erase 还是不错的,但是只能 erase 连续字符,如果要拿掉一个字符串里面所有的某个字符呢?用 STL 的 erase + remove_if 就可以了,注意光 remove_if 是不行的。

    string s("   hello, world. say bye   ");
    s.erase(remove_if(s.begin(),s.end(),
        bind2nd(equal_to<char>(), '' '')),
    s.end());

上面的这段会拿掉所有的空格,于是得到 hello,world.saybye。

 

4.replace
string 本身提供了 replace ,不过并不是面向字符串的,譬如我们最常用的把一个 substr 换成另一个 substr 的操作,就要做一点小组合:

    string s("hello, world");
    string sub("ello, ");
    s.replace(s.find(sub), sub.size(), "appy ");
    cout << s << endl;

输出为 happy world。注意原来的那个 substr 和替换的 substr 并不一定要一样长。

 

 

5.startwith, endwith
这两个可真常用,不过如果你仔细看看 string 的接口,就会发现其实没必要专门提供这两个方法,已经有的接口可以干得很好:

    string s("hello, world");
    string head("hello");
    string tail("ld");
    bool startwith = s.compare(0, head.size(), head) == 0;
    cout << boolalpha << startwith << endl;
    bool endwith = s.compare(s.size() - tail.size(), tail.size(), tail) == 0;
    cout << boolalpha << endwith << endl;

当然了,没有 s.startwith("hello") 这样方便。

 

 

6.toint, todouble, tobool...
    string s("123");
    int i = atoi(s.c_str());
    cout << i << endl;
   
    int ii;
    stringstream(s) >> ii;
    cout << ii << endl;
   
    string sd("12.3");
    double d = atof(sd.c_str());
    cout << d << endl;

 

 

double dd;
    stringstream(sd) >> dd;
    cout << dd << endl;
   
    string sb("true");
    bool b;
    stringstream(sb) >> boolalpha >> b;
    cout << boolalpha << b << endl;

 

7.切割字符串

#include <sstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
        string text = "big|dog|china|sonic|free";
        stringstream ss(text);
        string sub_str;
        while(getline(ss,sub_str,'|'))  //以|为间隔分割test的内容
                cout << sub_str << endl;

        return 0;
}
 

8.concat
把一个装有 string 的容器里面所有的 string 连接起来,怎么做?希望你不要说是 hand code 循环,这样做不是更好?

    vector<string> vect;
    vect.push_back("hello");
    vect.push_back(", ");
    vect.push_back("world");
   
    cout << accumulate(vect.begin(), vect.end(), string(""));


9.取文件的扩展名

std::string filename("hello.exe");

    std::string::size_type pos = filename.rfind(''.'');
    std::string ext = filename.substr(pos == std::string::npos ? filename.length() : pos + 1);

不过两行,合并成一行呢?也不是不可以:

    std::string ext = filename.substr(filename.rfind(''.'') == std::string::npos ? filename.length() : filename.rfind(''.'') + 1);

我知道,rfind 执行了两次。不过第一,你可以希望编译器把它优化掉,其次,扩展名一般都很短,即便多执行一次,区别应该是相当微小。

 

In file included from /usr/include/x86_64-linux-gnu/c++/8/bits/c++allocator.h:33, from /usr/include/c++/8/bits/allocator.h:46, from /usr/include/c++/8/string:41, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from main.cpp:7: /usr/include/c++/8/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::__cxx11::basic_string<char>; _Args = {const char&}; _Tp = std::_Rb_tree_node<std::__cxx11::basic_string<char> >]': /usr/include/c++/8/bits/alloc_traits.h:475:4: required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::__cxx11::basic_string<char>; _Args = {const char&}; _Tp = std::_Rb_tree_node<std::__cxx11::basic_string<char> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::_Rb_tree_node<std::__cxx11::basic_string<char> > >]' /usr/include/c++/8/bits/stl_tree.h:626:32: required from 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, _Args&& ...) [with _Args = {const char&}; _Key = std::__cxx11::basic_string<char>; _Val = std::__cxx11::basic_string<char>; _KeyOfValue = std::_Identity<std::__cxx11::basic_string<char> >; _Compare = Compare; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::__cxx11::basic_string<char> >*]' /usr/include/c++/8/bits/stl_tree.h:643:4: required from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const char&}; _Key = std::__cxx11::basic_string<char>; _Val = std::__cxx11::basic_string<char>; _KeyOfValue = std::_Identity<std::__cxx11::basic_string<char> >; _Compare = Compare; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::__cxx11::basic_string<char> >*]' /usr/include/c++/8/bits/stl_tree.h:2400:13: required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_equal(_Args&& ...) [with _Args = {const char&}; _Key = std::__cxx11::basic_string<char>; _Val = std::__cxx11::basic_string<char>; _KeyOfValue = std::_Identity<std::__cxx11::basic_string<char> >; _Compare = Compare; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::__cxx11::basic_string<char> >]' /usr/include/c++/8/bits/stl_multiset.h:458:63: required from 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::emplace(_Args&& ...) [with _Args = {const char&}; _Key = std::__cxx11::basic_string<char>; _Compare = Compare; _Alloc = std::allocator<std::__cxx11::basic_string<char> >; std::multiset<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<std::__cxx11::basic_string<char> >]' solution.cpp:34:43: required from here /usr/include/c++/8/ext/new_allocator.h:136:4: error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(const char&)' { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/8/string:52, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from main.cpp:7: /usr/include/c++/8/bits/basic_string.h:649:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::__sv_wrapper, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' basic_string(__sv_wrapper __svw, const _Alloc& __a) ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:649:7: note: candidate expects 2 arguments, 1 provided /usr/include/c++/8/bits/basic_string.h:639:2: note: candidate: 'template<class _Tp, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Tp&, const _Alloc&)' basic_string(const _Tp& __t, const _Alloc& __a = _Alloc()) ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:639:2: note: template argument deduction/substitution failed: /usr/include/c++/8/bits/basic_string.h:628:2: note: candidate: 'template<class _Tp, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Tp&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&)' basic_string(const _Tp& __t, size_type __pos, size_type __n, ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:628:2: note: template argument deduction/substitution failed: In file included from /usr/include/x86_64-linux-gnu/c++/8/bits/c++allocator.h:33, from /usr/include/c++/8/bits/allocator.h:46, from /usr/include/c++/8/string:41, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from main.cpp:7: /usr/include/c++/8/ext/new_allocator.h:136:4: note: candidate expects 4 arguments, 1 provided { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/8/string:52, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from main.cpp:7: /usr/include/c++/8/bits/basic_string.h:614:9: note: candidate: 'template<class _InputIterator, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&)' basic_string(_InputIterator __beg, _InputIterator __end, ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:614:9: note: template argument deduction/substitution failed: In file included from /usr/include/x86_64-linux-gnu/c++/8/bits/c++allocator.h:33, from /usr/include/c++/8/bits/allocator.h:46, from /usr/include/c++/8/string:41, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from main.cpp:7: /usr/include/c++/8/ext/new_allocator.h:136:4: note: candidate expects 3 arguments, 1 provided { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/8/string:52, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from main.cpp:7: /usr/include/c++/8/bits/basic_string.h:576:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' basic_string(basic_string&& __str, const _Alloc& __a) ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:576:7: note: candidate expects 2 arguments, 1 provided /usr/include/c++/8/bits/basic_string.h:572:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' basic_string(const basic_string& __str, const _Alloc& __a) ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:572:7: note: candidate expects 2 arguments, 1 provided /usr/include/c++/8/bits/basic_string.h:568:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::initializer_list<_Tp>, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:568:7: note: no known conversion for argument 1 from 'const char' to 'std::initializer_list<char>' /usr/include/c++/8/bits/basic_string.h:541:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' basic_string(basic_string&& __str) noexcept ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:541:7: note: no known conversion for argument 1 from 'const char' to 'std::__cxx11::basic_string<char>&&' /usr/include/c++/8/bits/basic_string.h:529:7: note: candidate: 'template<class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT, const _Alloc&)' basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:529:7: note: template argument deduction/substitution failed: In file included from /usr/include/x86_64-linux-gnu/c++/8/bits/c++allocator.h:33, from /usr/include/c++/8/bits/allocator.h:46, from /usr/include/c++/8/string:41, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from main.cpp:7: /usr/include/c++/8/ext/new_allocator.h:136:4: note: candidate expects 3 arguments, 1 provided { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/8/string:52, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from main.cpp:7: /usr/include/c++/8/bits/basic_string.h:514:7: note: candidate: 'template<class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&)' basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:514:7: note: template argument deduction/substitution failed: In file included from /usr/include/x86_64-linux-gnu/c++/8/bits/c++allocator.h:33, from /usr/include/c++/8/bits/allocator.h:46, from /usr/include/c++/8/string:41, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from main.cpp:7: /usr/include/c++/8/ext/new_allocator.h:136:4: note: cannot convert 'std::forward<const char&>((* & __args#0))' (type 'const char') to type 'const char*' { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/c++/8/string:52, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from main.cpp:7: /usr/include/c++/8/bits/basic_string.h:499:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]' basic_string(const _CharT* __s, size_type __n, ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:499:7: note: candidate expects 3 arguments, 1 provided /usr/include/c++/8/bits/basic_string.h:481:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]' basic_string(const basic_string& __str, size_type __pos, ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:481:7: note: candidate expects 4 arguments, 1 provided /usr/include/c++/8/bits/basic_string.h:465:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]' basic_string(const basic_string& __str, size_type __pos, ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:465:7: note: candidate expects 3 arguments, 1 provided /usr/include/c++/8/bits/basic_string.h:450:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]' basic_string(const basic_string& __str, size_type __pos, ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:450:7: note: candidate expects 3 arguments, 1 provided /usr/include/c++/8/bits/basic_string.h:437:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' basic_string(const basic_string& __str) ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:437:7: note: no known conversion for argument 1 from 'const char' to 'const std::__cxx11::basic_string<char>&' /usr/include/c++/8/bits/basic_string.h:429:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:429:7: note: no known conversion for argument 1 from 'const char' to 'const std::allocator<char>&' /usr/include/c++/8/bits/basic_string.h:420:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' basic_string() ^~~~~~~~~~~~ /usr/include/c++/8/bits/basic_string.h:420:7: note: candidate expects 0 arguments, 1 provided什么错误
06-07
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值