理解字符串

理解字符串

一、相关日志

String基本字符型序列容器

http://blog.163.com/zhoumhan_0351/blog/static/39954227201031112952296/

C++基础笔记(二)

http://blog.163.com/zhoumhan_0351/blog/static/39954227201012471245444/

C++基本概念(字符、字符串处理)

http://blog.163.com/zhoumhan_0351/blog/static/399542272010023101111576/

输入输出流(二)

http://blog.163.com/zhoumhan_0351/blog/static/39954227201003005237697/

二、理解字符串

1、In C++, individual string objects may or may not occupy unique physical regions 

of memory, but if reference counting avoids storing duplicate copies of data, the 

individual objects must look and act as though they exclusively own unique regions of 

storage.

只有当字符串修改时才创建自己的拷贝,称为写时复制。我们从他的构造函数中,可以看出创建一个string对象的方法。

template<class E,

    class T = char_traits<E>,

    class A = allocator<T> >

    class basic_string{

basic_string(const basic_string& s);//拷贝构造函数

basic_string(const basic_string& s, size_type pos, size_type n,

const A& al = A());//将string对象s的[pos,pos+n)中字符拷贝到新创建的string对象空间中,其中string::npos已定义为-1,当pos取值为0,n取值为string::npos时,就是用整个字符串s创建一个新的string对象。

basic_string(const E *s, size_type n, const A& al = A());//用字符串s的第n个字符来初始化string对象

basic_string(const E *s, const A& al = A());

basic_string(size_type n, E c, const A& al = A());//用n个字符c来初始化string对象

basic_string(const_iterator first, const_iterator last,

const A& al = A());//用[first,last)中字符串来初始化新的string

}

其常用成员函数有:

1)basic_string substr(size_type pos = 0,

    size_type n = npos) const;

返回[pos,pos+n+1]的字符串。

    不可以使用单个字符,ASCII或其它整数值来初始化string。

2)size_type capacity() const;

returns the storage currently allocated to hold the controlled sequence, a value at least 

as large as size().

3)void reserve(size_type n = 0);

The member function ensures that capacity() henceforth returns at least n.

    数据成员npos(no position)是string类的一个静态常量成员,表示一个不存在的字符位置。

The replace( ) algorithm only works with single objects (in this case, char 

objects) and will not replace quoted char arrays or string objects. Since a string 

behaves like an STL sequence, a number of other algorithms can be applied to it, 

which might solve other problems that are not directly addressed by the string 

member functions.

4)const E *c_str() const;

returns a pointer to a nonmodifiable C string constructed by adding a terminating null 

element (E(0)) to the controlled sequence. 

    如果想把一个字符串传送给一个标准C语言函数时,const char*可以有用场。

2、自己重写string

//: C03:ichar_traits.h

// Creating your own character traits.

#ifndef ICHAR_TRAITS_H

#define ICHAR_TRAITS_H

#include <cctype>

#include <cmath>

#include <cstddef>

#include <ostream>

#include <string>

using std::allocator;

using std::basic_string;

using std::char_traits;

using std::ostream;

using std::size_t;

using std::string;

using std::toupper;

using std::tolower;

struct ichar_traits : char_traits<char> {

  // We'll only change character-by-

  // character comparison functions

  static bool eq(char c1st, char c2nd) {

    return toupper(c1st) == toupper(c2nd);

  }

  static bool ne(char c1st, char c2nd) {

    return !eq(c1st, c2nd);

  }

  static bool lt(char c1st, char c2nd) {

    return toupper(c1st) < toupper(c2nd);

  }

  static int

  compare(const char* str1, const char* str2, size_t n) {

    for(size_t i = 0; i < n; ++i) {

      if(str1 == 0)

        return -1;

      else if(str2 == 0)

        return 1;

      else if(tolower(*str1) < tolower(*str2))

        return -1;

      else if(tolower(*str1) > tolower(*str2))

        return 1;

      assert(tolower(*str1) == tolower(*str2));

      ++str1; ++str2; // Compare the other chars

    }

    return 0;

  }

  static const char*

  find(const char* s1, size_t n, char c) {

    while(n-- > 0)

      if(toupper(*s1) == toupper(c))

        return s1;

      else

        ++s1;

    return 0;

  }

};

typedef basic_string<char, ichar_traits> istring;

inline ostream& operator<<(ostream& os, const istring& s) {

  return os << string(s.c_str(), s.length());

}

#endif // ICHAR_TRAITS_H ///:~

//: C03:ICompare.cpp

#include <iostream>

//#include "ichar_traits.h"

using namespace std;

int main() {

  // The same letters except for case:

  istring first = "tHis";

  istring second = "ThIS";

  cout << first << endl;

  cout << second << endl;

 if(first==second) cout<<"EQ";

} ///:~

    The <string> header provides a wide string class via the following typedef:

typedef basic_string<wchar_t> wstring;

Wide string support also reveals itself in wide streams (wostream in place of 

ostream, also defined in <iostream>) and in the header <cwctype>, a wide-character 

version of <cctype>. This along with the wchar_t specialization of char_traits in the 

standard library allows us to do a wide-character version of ichar_traits

注意:string str="12""23";会把两个字符串合成一个。str为1223.

The string class member functions provide a fairly comprehensive set of tools for 

creating, modifying, and searching in strings. String comparisons are always case 

sensitive, but you can work around this by copying string data to C-style 

null-terminated strings and using case-insensitive string comparison functions, 

temporarily converting the data held in string objects to a single case, or by creating a

 case-insensitive string class that overrides the character traits used to create the 

basic_string object.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值