1. string的定义原型

    typedef basic_string<char, char_traits<char>, allocator<char> >
    string;
    typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >
    wstring; // 第二三个参数有默认值

  2. string部分构造方式
    // string 部分构造方式
    #include <iostream>
    #include <string>
    using namespace std; int main(void)
    {
    string s1;
    string s2("ABCDEFG");
    cout << s1 << endl;
    cout << s2 << endl; // 使用最初的形式定义
    basic_string<char> s3("xxxx");
    cout << s3 << endl; /*
    basic_string(
    const value_type *_Ptr,
    size_type _Count
    );
    */
    string s4("DEFG", 2);
    cout << s4 << endl; // DE
    /*
    basic_string(
    const basic_string& _Right,
    size_type _Roff,
    size_type _Count = npos
    );
    */
    string s5(s2, 2, 2);
    cout << s5 << endl; //CD /*
    basic_string(
    const_iterator _First,
    const_iterator _Last
    );
    */
    string::const_iterator first = s2.begin() + 1;
    string::const_iterator end = s2.begin() + 3; // [first, end)
    string s6(first, end);
    cout << s6 << endl; // BC return 0;
    }

  3. string常用成员函数
    // string 部分成员函数部分形式操作
    #include <iostream>
    #include <string>
    using namespace std; int main(void)
    {
    string s1("ABCDEC");
    cout << s1.size() << endl;
    cout << s1.length() << endl; cout << s1.empty() << endl;
    // 一样第二个参数是count,不是截至位置
    string s2 = s1.substr(1, /*4*/string::npos); // string::npos == 4294967295
    cout << s2 << endl; //find
    /*
    size_type find(
    value_type _Ch,
    size_type _Off = 0
    ) const;
    */
    // 查找某个字符在string中第一次出现的位置(下标),没找到返回 4294967295 最大值
    cout << s1.find('C') << endl; // 2
    //size_type -> size_t ->unsigned int
    /*
    size_type find(
    const value_type* _Ptr,
    size_type _Off = 0
    ) const;
    */
    // 在string中查找字符串,找到返回第一次出现位置的下标,没找到返回 string::npos, 4294967295 最大值
    cout << s1.find("CD") << endl; // 2
    /*
    size_type find(
    const basic_string<CharType, Traits, Allocator>& _Str,
    size_type _Off = 0
    ) const;
    */
    // 在string中查找string
    cout << s1.find(s2) << endl; //rfind,反向查找,返回第一次查找到的下标
    cout << s1.rfind('C') << endl; // 5 //replace
    /*
    basic_string<CharType, Traits, Allocator>& replace(
    size_type _Pos1,
    size_type _Num1,
    const value_type* _Ptr
    );
    */
    // 第三个参数也能是string,这里不写了,只介绍这一个好了
    string s3 = s1;
    s3.replace(1, 2, "RRR");
    cout << s3 << endl; // ARRRDEC // compare
    cout << s3.compare(s1) << endl; // 1, s3 > s1 // insert
    /*
    basic_string<CharType, Traits, Allocator>& insert(
    size_type _P0,
    const value_type* _Ptr
    );
    */
    s3.insert(2, "aaaaaaaaaaa");
    cout << s3 << endl; // ARaaaaaaaaaaaRRDEC // append
    /*
    basic_string<CharType, Traits, Allocator>& append(
    const value_type* _Ptr
    );
    */
    s3.append("OOOOOOOOOOOOOOOOO");
    cout << s3 << endl; // ARaaaaaaaaaaaRRDECOOOOOOOOOOOOOOOOO // swap
    /*
    void swap(
    basic_string<CharType, Traits, Allocator>& _Str
    );
    */
    s3.swap(s2);
    cout << s3 << endl;
    cout << s2 << endl; return 0;
    }

  4. string综合应用
      在字符集中查找存在的首尾字符,并截取。
    #include <iostream>
    #include <string>
    using namespace std; int main()
    {
    string strinfo = " //*---------Hello World!--------------*//";
    string strset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    /*
    size_type find_first_of(
    const basic_string<CharType, Traits, Allocator>& _Str,
    size_type _Off = 0
    ) const;
    */
    string::size_type first = strinfo.find_first_of(strset);
    if (first == string::npos)
    cout << "not found." << endl;
    string::size_type end = strinfo.find_last_of(strset);
    if (end == string::npos)
    cout << "not found." << endl;
    //cout << "string::npos" << string::npos << endl; // string::npos == 4294967295
    cout << strinfo.substr(first, end - first + 1) << endl; return 0;
    }

  5. string简单去除字符串左右空格
    // string 简单实现去除字符串左右空格
    #include <iostream>
    #include <string>
    using namespace std; class StringUtils {
    public:
    static void LTrim(string &s)
    {
    string drop = " \t";
    s.erase(0, s.find_first_not_of(drop));
    } static void RTrim(string &s)
    {
    string drop = " \t";
    s.erase(s.find_last_not_of(drop)+1);
    } static void Trim(string &s)
    {
    LTrim(s);
    RTrim(s);
    }
    }; int main(void)
    {
    string s1 = " ABCD ";
    StringUtils::Trim(s1);
    cout << s1 << endl; // "ABCD"
    cout << s1.size() << endl; // 4 return 0;
    }

http://www.cnblogs.com/ifpelset/articles/4523925.html

std::string 简单入门的更多相关文章

  1. 标准C++类std::string的内存共享和Copy-On-Write...

    标准C++类std::string的 内存共享和Copy-On-Write技术 陈皓 1. 概念 Scott Meyers在<More Effective C++>中举了个例子,不知你是否 ...

  2. 【转】标准C++类std::string的内存共享和Copy-On-Write技术

    1.             概念 Scott Meyers在<More Effective C++>中举了个例子,不知你是否还记得?在你还在上学的时候,你的父母要你不要看电视,而去复习功 ...

  3. 标准C++类std::string的内存共享和Copy-On-Write技术

    标准C++类std::string的  内存共享和Copy-On-Write技术 陈皓 1. 概念 Scott Meyers在<More Effective C++>中举了个例子,不知你是 ...

  4. [原创]MYSQL的简单入门

    MYSQL简单入门: 查询库名称:show databases; information_schema mysql test 2:创建库 create database 库名 DEFAULT CHAR ...

  5. could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'

    VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...

  6. Okio 1.9简单入门

    Okio 1.9简单入门 Okio库是由square公司开发的,补充了java.io和java.nio的不足,更加方便,快速的访问.存储和处理你的数据.而OkHttp的底层也使用该库作为支持. 该库极 ...

  7. 【java开发系列】—— spring简单入门示例

    1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...

  8. C++: std::string 与 Unicode 结合

    一旦知道 TCHAR 和_T 是如何工作的,那么这个问题很简单.基本思想是 TCHAR 要么是char,要么是 wchar_t,这取决于_UNICODE 的值: // abridged from tc ...

  9. C++: std::string 与 Unicode 如何结合?

    关键字:std::string Unicode 转自:http://www.vckbase.com/document/viewdoc/?id=1293 一旦知道 TCHAR 和_T 是如何工作的,那么 ...

随机推荐

  1. eclipse插件安装验证及问题处理

    eclipse插件安装验异常时可看当前workspace下面的.metadata/.log文件,找到具体的问题来处理.一般常用到插件安装不成功的原因如下: 1.jar包冲突: 2.jar包依赖的jav ...

  2. 结合Wireshark捕获分组深入理解TCP/IP协议之以太网帧

    摘要:     本文摘抄并整理了以太网相关理论知识,包括CSMA/CD协议机制及工作.LAN互连,详细分析了Ethernet II帧格式,最后给出Ethernet II帧实例. 一.以太网[1] 1. ...

  3. 机器学习算法笔记1_2:分类和逻辑回归(Classification and Logistic regression)

    形式: 採用sigmoid函数: g(z)=11+e−z 其导数为g′(z)=(1−g(z))g(z) 如果: 即: 若有m个样本,则似然函数形式是: 对数形式: 採用梯度上升法求其最大值 求导: 更 ...

  4. Eclipse RCP 中创建自己定义首选项,并能读取首选项中的值

    Eclipse RCP的插件中若想自定义首选项须要扩展扩展点: org.eclipse.core.runtime.preferences //该扩展点用于初始化首选项中的值 org.eclipse.u ...

  5. 高速在MyEclipse中打开jsp类型的文件

    MyEclipse打开jsp时老是要等上好几秒,嗯嗯,这个问题的确非常烦人,事实上都是MyEclipse的"自作聪明"的结果(它默认用Visual Designer来打开的),进行 ...

  6. iOS进阶路线以及进阶书籍

    第一,熟悉ARC机制:首先要了解ARC的前世今生.假设了解不清楚会导致两种可能,1,一个对象的引用莫名奇异为空.或失效了.这个一般都能在开发阶段及时发现,由于会导致应用异常.2.导致内存溢出:不了解A ...

  7. thinkphp 获取url参数,汉字获取不到

    //这样获取不到 dump($_GET['car_code']); //这样就可以获取了 dump(iconv("gb2312","utf-8",$_GET[' ...

  8. 2016 Java程序员的年终总结(转)

    2016 Java程序员的年终总结 技术积累 (1)代码规范 1.1.1.通常的模块分布:一般如果你要实现一个web 应用,你从后台将数据展示到前端页面,在一个比较大的公司,你少不了跟其他项目有交集( ...

  9. [JS Compose] 1. Refactor imperative code to a single composed expression using Box

    After understanding how Box is, then we are going to see how to use Box to refacotr code, to un-nest ...

  10. try~Catch语句中异常的处理过程

    [2014/10/12 21:40]文章待续~ 1.函数自身捕获处理异常的情况 以下的样例介绍了try~catch语句中出现异常时语句的运行顺序: package month10; import ja ...