标准库类型之string

上几篇中已经实现了一个简单版的String字符串类,但是实际开发中不用我们自己实现了,学习Java的也知道有一个系统现成的用,当然强大的C++也不例外,下面就来学习一下系统定义的string是怎么用的。

  • string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作。
  • 它有两种形式:
      typedef basic_string<char> string;
      typedef basic_string<wchar_t> wstring; 它是支持宽字符的~
  • 要使用string类型对象,必须包含相关头文件 
    #include <string>
    using std::string;

 实际上string属于STL标准模板库中的范畴,虽然还没学到模板,这个会在之后系统的学习,但是不影响学习,像上面出现的basic_string<char>和basic_string<wchar_t>就是一个模板类,只要知道string是一个模板类既可,其实用的时候可以将它当正常的类使用,下面来使用一下:


以下几种全为它的构造形式,只是一小部份,它还有很多构造形式,具体可以查看帮助文档:

  • string s1;   //默认构造函数,s1为空串
  • string s2(s1);   //将s2初始化为s1的一个副本
  • string s3(“value”);   //将s3初始化为一个字符串字面值副本
  • string s4(n, ‘c’);  //将s4初始化为字符‘c’的n个副本

编译运行:

对于string到底有哪一些函数,需要学会使用帮助,就像学java时也需要看JDK帮助文档一样:在studio中按F11既可查看【如果没有安装本地的帮助,可以上官网查询:https://msdn.microsoft.com/en-us/library/3bstk3k5.aspx

其中string就是basic_string,可以查看一下它:

所以其实代码可以这样写:

编译运行:

下面则来查看一下basic_string类的帮助:

查击可以看到它有N多种构造形式:

basic_string(
   const value_type* _Ptr, 
   size_type _Count, 
   const allocator_type& _Al = Allocator ( )
); 
basic_string(
   const value_type* _Ptr, 
   const allocator_type& _Al = Allocator ( )
); 
basic_string(
   const basic_string& _Right, 
   size_type _Roff, 
   size_type _Count = npos,
   const allocator_type& _Al = Allocator ( )
);
basic_string(
   size_type _Count,
   value_type _Ch,
   const allocator_type& _Al = Allocator ( )
);
explicit basic_string(
   const allocator_type& _Al = Allocator ( )
);
template<class _It>
   basic_string(
      _It _First,
      _It _Last,
      const allocator_type& _Al = Allocator ( )
);
basic_string(
   const_pointer _First,
   const_pointer _Last
);
basic_string(
   const_iterator _First,
   const_iterator _Last
);

那对于目前我们使用的带一个参数的是哪种形式呢?

代码如下:

编译运行:

明白了查看帮助文档,那学习其它形式的构造函数就比较容易了,下面再来使用一下其它构造:

编译运行:

下面继续再来介绍一个带迭待器的构造,如下:

编译运行:

那结果是多少呢?

是不是跟预想的不一样,因为它是一个闭开区间(包含first位置上的字符,不包含last位置上的字符),而为什么s2.end()可以包含最后一个字符呢?因为它的位置是在最后一个字符后面,这个需要注意。

下面用代码来使用一下喽,对于有编程经验的人来说其实挺简单的:

结果:

其函数的使用参数可以参考文档:

还有这种形式的:

下面也来试一下:

正如之前实现的自己的String类一样,相加运算一定是前面两个有一个为对象才行,上面修改如下:

另外字符串中有一个很重要并且实际中会经常要用到的一个成员,如下:

不行,那么接下来这个成员函数就派上用场了:

关于这个错误应该比较有经验了,去掉常量性既可:

最后再介绍四个函数:

下面直接看程序:

#include <string>
#include <iostream>
using namespace std;
int main()
{
    string strinfo=" //*---Hello World!......------";
    string strset= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    string::size_type first = strinfo.find_first_of(strset);//在strinfo中查找第一个在strset时面的字符位置,也就是'H',在strinfo中是第7位
    if(first == string::npos) 
        cout<<"not find any characters"<<endl; 
    string::size_type last = strinfo.find_last_of(strset);//在strinfo中查找最后一个在strset时面的字符位置,也就是'd',在strinfo中是第17位
    if(last == string::npos)
        cout<<"not find any characters"<<endl;
    cout << strinfo.substr(first, last - first + 1)<<endl;//闭开区间来截取字符
    return 0;
}

结果是:

#include <string>
#include <iostream>
using namespace std;
int main()
{
    string strinfo=" //*---Hello World!......------";
    string strset= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    string::size_type first = strinfo.find_first_not_of(strset);//在strinfo中查找第一个不在strset时面的字符位置,也就是' ',在strinfo中是第0位
    if(first == string::npos) 
        cout<<"not find any characters"<<endl; 
    string::size_type last = strinfo.find_last_of(strset);//在strinfo中查找最后一个在strset时面的字符位置,也就是'd',在strinfo中是第17位
    if(last == string::npos)
        cout<<"not find any characters"<<endl;
    cout << strinfo.substr(first, last - first + 1)<<endl;//闭开区间来截取字符
    return 0;
}

#include <string>
#include <iostream>
using namespace std;
int main()
{
    string strinfo=" //*---Hello World!......------";
    string strset= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    string::size_type first = strinfo.find_first_not_of(strset);//在strinfo中查找第一个不在strset时面的字符位置,也就是' ',在strinfo中是第0位
    if(first == string::npos) 
        cout<<"not find any characters"<<endl; 
    string::size_type last = strinfo.find_last_not_of(strset);//在strinfo中查找最后一个不在strset时面的字符位置,也就是'-',在strinfo中是最后一位
    if(last == string::npos)
        cout<<"not find any characters"<<endl;
    cout << strinfo.substr(first, last - first + 1)<<endl;//闭开区间来截取字符
    return 0;
}

这四个函数有什么作用呢?可以用这四个函数来实现这个功能:实现一个字符串去除左右空格~

关于这个功能的编写在下编中再见~

posted on 2016-05-10 22:36  cexo  阅读(221)  评论(0编辑  收藏  举报

导航