string

/*****************************
这是一个学习的工程,专门学习c++的string,也许你会觉得用c语言的 char *用的挺好的
但是,如果你真正认识了string后你就不会再坚持要用char *来处理字符串了。这个工程的
每一个文件都是针对string的一个方面来的,我希望能在每一个相关的文件中把其牵涉到的
知识解释清楚。
要熟练的掌握一门编程语言,研习运用库是很重要的。
本文件是测试文件

  www.54sh.com
  made by efish
  06.03.31

  下面首先从总体上来说明string的用法
  1.what is a string?
 
 .c++ string hide the physical representation of the sequence of characters they contain
 
 .a c++ string object knows its starting location in memory, its content, its length in characters
  ,and the length in characters
 
 .indiviual 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

  2.books and online helps for c++ standard library.
 
 <<the c++ programming language>>
 <<the c++ standard library>>
 <<dinkumware c/c++ library reference>> http:// www.dinkumware.com

  3.what can you do with string

 create and initializing c++ strings("strinit.h")
 oparating on strings
 appending ,insterting and concatenating strings
 replacing string characters
 concatenation using nonmember overloaded operators
 searching in strings
 finding in reverse
 finding first/last of a set of characters
 removing characters from strings
 comparing strings
******************************/

#include "strinit.h"
#include "ichar_traits.h"
#include <iostream>
#include <string>
#include <cassert>
using namespace std;

int main(int argc, char *argv[])
{
 strinit();//string的初始化
 //不区分大小写的字符集的测试
 istring first = "tHis";
 istring second = "thIs";
 cout<<first<<endl;
 cout<<second<<endl;
 assert(first.compare(second) == 0);
 assert(first.find('h') == 1);
 assert(first.find('I') == 2);
 assert(first.find('x') == string::npos);
 return 0;
}


/**********************************
这个文件演示了怎么初始化一个string

www.54sh.com
made by efish
06.03.31
**********************************/
#ifndef STRINIT_H
#define STRINIT_H
#include <string>
#include <iostream>
#include <cassert>

using std::string;
using std::endl;
using std::cout;

void strinit()
{
 //1.create an empty string and defer initializing it with character data
 string blank;
 //2.initialize a string by passing a literal,quoted character as an argument to the constructor
 string bey("good bey!");
 //3.using the equal sign (=)
 string repaly = "good bey too!";
 //4.use one string to initialize another
 string rereplay(repaly);
 //5.use a portion of either a c char array or a c++ string
 //6.combine different sources of initialization data using operator +
 //7.use the string object's substr() member function to create a substring
 string s1("what is the sound of one clam napping?");
 string s2("anything worth doing is worth overdoing.");
 string s3("i saw elvis in a ufo");
 //copy the first 8 chars
 string s4(s1,0,8);
 cout<<s4<<endl;
 //copy 6 chars from the middle of the source
 string s5(s2,15,6);
 cout<<s5<<endl;
 //copy from middle to end
 string s6(s3,6,37);
 cout<<s6<<endl;
 //copy many different things
 string s7 = s4 + "that with" + s3.substr(5,100) + s1.substr(37,1);
 cout<<s7<<endl;

 //8.use string iterators
 string source("XXX:");
 string s(source.begin(),source.end());
 assert(s == source);
 string s8(source.begin()+2, source.end());
 cout<<s8<<endl;

 //9.copies of a single character
 string s9(100,'a');
 cout<<s9<<endl;
}
#endif

/******************************
trim.h
find 函数的使用
******************************/

#ifndef TRIM_H
#define TRIM_H
#include <string>
#include <cstddef>

/****************************
//把string s前后的空格去掉
****************************/
inline std::string trim(const std::string &s)
{
 if(s.length == 0)//如果长度为零直接返回
  return s;
 std::size_t beg = s.find_first_not_of(" /a/n/b/f/r/t/v");//找到第一个不是空格的字符的位置
 std::size_t end = s.find_last_not_of(" /a/b/f/n/r/t/v");//找到最后一个不是空格的字符位置
 if(beg == std::string::npos)//没找到,返回空
  return "";
 return std::string(s,beg,end-beg+1);//把前后的空格去掉后返回
}
 
#endif

 

/********************************
ichar_traits.h
这个文件测试了c++是怎么提供对其他字符集的支持的

为什么要提供对其他字符集的支持呢?
这一点非常重要,因为我们要经常用到,特别是对于我们使用中文的朋友,管硬说还不行,举一连个吧:

  1.我们经常要用到不需要区分大小写的情况,比如说DOS命令,DOS文件路径,html文档的标签等等,如果
  用通常的string来处理这类问题是很头痛的。(就一个《img>标签你得考虑n种情况,如<Img><IMg>...)

  2.使用非英语时,这种情况太常见了
当然还有其他的,暂时还没想到,不过此时我已经觉的c++提供对其他字符集的支持是very important的了

怎么支持?

  先来看一下我们的string类是如何定义的
   typedef basic_string<char> string;
他只不过是basic_string模板的一个别名罢了,关键是basic_string
template<class char T, class traits = char_traits<charT>,class allocator = allocator<charT>>
class basic_string;
关于模板的详细细节我不是很清楚,但是我们只要构造一个 char_traits<>的类,并提供成员函数如eq()
,ne(),lt(),find(),compare()等等,就可以支持其他的字符集了

  另外c++标准模板库中直接提供了对宽字符的支持,在<string>里面有下面的typedef
  typedef basic_string<wchar_t> wstring;
  同时在<ostream>里有wostream的定义,<cwctype>等等

  不过好像我还是没说清楚,下面用代码来说明问题。本例实现的是对不区分大小写的字符集的支持
********************************/
#ifndef ICHAR_TRAITS_H
#define ICHAR_TRAITS_H

#include <cassert>
#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::string;
//using  std::size_t;
//using  std::toupper;
//using  std::tolower;

struct ichar_traits : char_traits<char>// 结构体的继承?
{
 //下面定义不区分大小写的字符的比较
 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);
 }
 //比较两个字符串str1,str2比较n个单位
 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;
  }
  return 0;
 }
 //查找
 static const char * find(const char* str1, size_t n, char c)
 {
  while(n-- > 0)
   if(toupper(*str1) == toupper(c))
    return str1;
   else
    ++ str1;
  return 0;
 }
};

//下面定义一个typedef 就是不区分大小写的字符集的string
typedef basic_string<char,ichar_traits> istring;

//定一个输出流
inline ostream& operator <<(ostream& os, const istring& is)
{
 return os << string(is.c_str(),is.length());
}
#endif

 

/***************************
iwchar_traits.h
本头文件提供了对宽字符不区分大小写的字符集的支持
详细介绍请看ichar_traits.h
***************************/
#ifndef IWCHAR_TRAITS_H
#define IWCHAR_TRAITS_H

#include <cassert>
#include <cwctype>
#include <ostream>
#include <string>
#include <cstddef>
#include <cmath>

using std::wstring;
using std::wostream;
using std::allocator;
using std::basic_string;
using std::char_traits;

//using std::size_t;
//using std::towlower;
//using std::towupper;

struct ichar_traits : char_traits<wchar_t>
{
 //单个字符的比较函数
 static bool eq(wchar_t c1,wchar_t c2)
 {
  return towupper(c1) == towupper(c2);
 }
 static bool ne(wchar_t c1, wchar_t c2)
 {
  return towupper(c1) != towupper(x2);
 }
 static bool lt(wchar_t c1,wchar_t c2)
 {
  return towupper(c1) < towupper(c2);
 }
 //字符串的比较,比较n个单元
 static int compate(const wchar_t *str1,const wchar_t *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(towlower(*str1) < towlower(*str2)) return -1;
   else if(towlower(*str1) > towlower(*str2)) return 1;
   assert(towlower(*str1) == towlower(*str2));
   ++ str1;++ str2;//比较下一个字符
  }
  return 0;
 }
 //查找
 static const wchar_t * find(const wchar_t *str1,size_t n,wchar_t c)
 {
  while(n-- >0)
   if(towlower(*str1) == towlower(c))
    return str1;
   else
    ++ str1;
  return 0;
 }
}
//类型的定义
typedef basic_string<wchar_t, iwchar_traits> iwstring;

inline wostream& operator<<(wostream& os, const iwstring& s)
{
 return os << wstring(s.c_str(), s.length());
}
#endif

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值