×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

i am not a C++ guy, just did a little investiagtion for your reference. http://www.halpernwightsoftware.com/stdlib-scratch/quickref.html#strings2

本文发表在 rolia.net 枫下论坛The string Class
#include <string>
class string;
String Nested Types and Constants
The following types are nested within the string class:
Types

size_type
difference_type
iterator
const_iterator
reverse_iterator
const_reverse_iterator

Constants

const size_type npos; // Maximum size_type

String Member Functions
Constructors

string();
string(const string& s2); // Copy constructor
string(const string& s2, size_type pos2, size_type len2);
string(const char* nts); // convert from C-string
string(const char* buf, size_type bufsize);
string(size_type repetitions, char c);

Assignment Operators and Functions

string& operator=(const string& s2); // normal assignment
string& operator=(const char* nts); // convert from C-string
string& operator=(char c); // assign a single character

string& assign(const string& s2);
string& assign(const string& s2, size_type pos2, size_type len2);
string& assign(const char* nts);
string& assign(const char* buf, size_type buflen);
string& assign(size_type repetitions, char c);

void swap(string& s2);

Iterator Functions

iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
reverse_iterator rbegin();
reverse_iterator rend();
const_reverse_iterator rbegin() const;
const_reverse_iterator rend() const;

Size and Capacity

size_type size() const;
size_type length() const; // same as size()
size_type max_size() const;
void resize(size_type size, char c = '\0');
void clear();
bool empty() const;

size_type capacity() const;
void reserve(size_type capacity = 0);

Element Access

char& operator[](size_type pos);
const char& operator[](size_type pos) const;
char& at(size_type pos);
const char& at(size_type pos) const;

Append Functions and Operators

string& append(const string& s2);
string& append(const string& s2, size_type pos2, size_type len2);
string& append(const char* nts);
string& append(const char* buf, size_type buflen);
string& append(size_type repetitions, char c);

string& operator+=(const string& s2);
string& operator+=(const char* nts);
string& operator+=(char c);

Insert Functions

string& insert(size_type pos1, const string& s2);
string& insert(size_type pos1, const string& s2,
size_type pos2, size_type len2);
string& insert(size_type pos1, const char* nts);
string& insert(size_type pos1, const char* buf, size_type buflen);
string& insert(size_type pos1, size_type repetitions, char c);

iterator insert(iterator pos1, char c);
void insert(iterator pos1, size_type repetitions, char c);

Erase Functions

string& erase(size_type pos = 0, size_type len = npos);
iterator& erase(iterator pos);
iterator& erase(iterator start, iterator finish);

Replace Functions

string& replace(size_type pos1, size_type len1, const string& s2);
string& replace(size_type pos1, size_type len1, const string& s2,
size_type pos2, size_type len2);
string& replace(size_type pos1, size_type len1, const char* nts);
string& replace(size_type pos1, size_type len1,
const char* buf, size_type buflen);
string& replace(size_type pos1, size_type len1,
size_type repetitions, char c);

string& replace(iterator start, iterator finish, const string& s2);
string& replace(iterator start, iterator finish, const char* nts);
string& replace(iterator start, iterator finish,
const char* buf, size_type buflen);
string& replace(iterator start, iterator finish,
size_type repetitions, char c);

Comparison Functions

// compare function returns -1 if *this < s2, 0 if *this == s2
// and +1 if *this > s2.
int compare(const string& s2) const;
int compare(size_type pos1, size_type len1, const string& s2);
int compare(size_type pos1, size_type len1, const string& s2,
size_type pos2, size_type len2);
int compare(const char* nts);
int compare(size_type pos1, size_type len1, const char* nts);
int compare(size_type pos1, size_type len1,
const char* buf, size_type buflen);

Substrings

string substr(pos = 0, len = npos) const;

// This constructor is repeated here because it creates substring
string(const string& s2, size_type pos2, size_type len2);

Conversions

// This constructor is repeated here because it converts
// a C string to a C++ string.
string(const char* nts);

const char* c_str() const; // Returns a null-terminated string
const char* data() const; // Not null-terminated

// Copy characters to an array. Does not append a null-terminator
size_type copy(char* buf, size_type bufsize, size_type pos1 = 0) const;

Search Functions

size_type find(const string& s2, size_type pos1);
size_type find(const char* nts, size_type pos1);
size_type find(const char* buf, size_type pos1, size_type bufsize);
size_type find(char c, size_type pos1);

size_type rfind(const string& s2, size_type pos1);
size_type rfind(const char* nts, size_type pos1);
size_type rfind(const char* buf, size_type pos1, size_type bufsize);
size_type rfind(char c, size_type pos1);

size_type find_first_of(const string& s2, size_type pos1);
size_type find_first_of(const char* nts, size_type pos1);
size_type find_first_of(const char* buf, size_type pos1, size_type bufsize);
size_type find_first_of(char c, size_type pos1);

size_type find_last_of(const string& s2, size_type pos1);
size_type find_last_of(const char* nts, size_type pos1);
size_type find_last_of(const char* buf, size_type pos1, size_type bufsize);
size_type find_last_of(char c, size_type pos1);

size_type find_first_not_of(const string& s2, size_type pos1);
size_type find_first_not_of(const char* nts, size_type pos1);
size_type find_first_not_of(const char* buf, size_type pos1,
size_type bufsize);
size_type find_first_not_of(char c, size_type pos1);

size_type find_last_not_of(const string& s2, size_type pos1);
size_type find_last_not_of(const char* nts, size_type pos1);
size_type find_last_not_of(const char* buf, size_type pos1,
size_type bufsize);
size_type find_last_not_of(char c, size_type pos1);

String-Related Global Operators and Functions
The following functions and operators are related to the string class but are not members of string. Along with the string class, the following definitions are in the <string> header:

// String concatenation:
string operator+(const string& s1, const string& s2);
string operator+(const char* nts, const string& s2);
string operator+(char c, const string& s2);
string operator+(const string& s1, const char* nts);
string operator+(const string& s1, char c);

// Equal
bool operator==(const string& s1, const string& s2);
bool operator==(const char* nts, const string& s2);
bool operator==(const string& s1, const char* nts);

// Not-equal
bool operator!=(const string& s1, const string& s2);
bool operator!=(const char* nts, const string& s2);
bool operator!=(const string& s1, const char* nts);

// Ordering:
bool operator<(const string& s1, const string& s2);
bool operator<(const char* nts, const string& s2);
bool operator<(const string& s1, const char* nts);

// Greater than:
bool operator>(const string& s1, const string& s2);
bool operator>(const char* nts, const string& s2);
bool operator>(const string& s1, const char* nts);

// Less-than or equal-to:
bool operator<=(const string& s1, const string& s2);
bool operator<=(const char* nts, const string& s2);
bool operator<=(const string& s1, const char* nts);

// Greater-than or equal-to:
bool operator>=(const string& s1, const string& s2);
bool operator>=(const char* nts, const string& s2);
bool operator>=(const string& s1, const char* nts);

// Input and output
ostream& operator<<(ostream& os, const string& s);
istream& operator>>(istream& is, string& s);
istream& getline(istream& is, string& s, char delimiter = '\n');更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / C++ 有没有string类型?我查帮助有,但当我用C++ 编译器编译时,显示错误没有这种类型。请指点。
    C++ 有没有string类型?我查帮助有,但当我用C++ 编译器编译时,显示错误没有这种类型。请指点。我用的不是VC++or C++ builder, 我从网上下载了一个C++ 编译器。
    • 看看相应的头文件目录有没有string
      • 谢谢回复。有一个文件string.h, 我把它包含进我的源程序中,但无济于事 。
        • Try CString
          • 我记得CString 是VC++ 的类,C++ 中应该没有这个类
        • 看看这个文件里面有没有类似的类
    • Are you talking about data type or class?? 类型????
      • sorry to let you confused. i am talking about class(C++)
        • I remember that class string is part of STL. You can define it yourselft
          • if i define it myself, i have to define all the functions.do you know which *.h include its define?thanks
            • string.hpp or simply string
            • C++ 一般用char *来定义string, CString 是MFC里面的类型, 如果你没有用MFC, 就不应该用CString. 当然你也可以用string, include iostream.h and string.h.刚看到你的帖子, 不知道你的问题解决了没有.
              • STL现在已经被包括在C++标准里面了,同样,string现在也已经在C++里面了。C++里用到char*的机会越来越少了,char*不是很好用,特别是对于不熟悉的人来说。
    • i am not a C++ guy, just did a little investiagtion for your reference. http://www.halpernwightsoftware.com/stdlib-scratch/quickref.html#strings2
      本文发表在 rolia.net 枫下论坛The string Class
      #include <string>
      class string;
      String Nested Types and Constants
      The following types are nested within the string class:
      Types

      size_type
      difference_type
      iterator
      const_iterator
      reverse_iterator
      const_reverse_iterator

      Constants

      const size_type npos; // Maximum size_type

      String Member Functions
      Constructors

      string();
      string(const string& s2); // Copy constructor
      string(const string& s2, size_type pos2, size_type len2);
      string(const char* nts); // convert from C-string
      string(const char* buf, size_type bufsize);
      string(size_type repetitions, char c);

      Assignment Operators and Functions

      string& operator=(const string& s2); // normal assignment
      string& operator=(const char* nts); // convert from C-string
      string& operator=(char c); // assign a single character

      string& assign(const string& s2);
      string& assign(const string& s2, size_type pos2, size_type len2);
      string& assign(const char* nts);
      string& assign(const char* buf, size_type buflen);
      string& assign(size_type repetitions, char c);

      void swap(string& s2);

      Iterator Functions

      iterator begin();
      iterator end();
      const_iterator begin() const;
      const_iterator end() const;
      reverse_iterator rbegin();
      reverse_iterator rend();
      const_reverse_iterator rbegin() const;
      const_reverse_iterator rend() const;

      Size and Capacity

      size_type size() const;
      size_type length() const; // same as size()
      size_type max_size() const;
      void resize(size_type size, char c = '\0');
      void clear();
      bool empty() const;

      size_type capacity() const;
      void reserve(size_type capacity = 0);

      Element Access

      char& operator[](size_type pos);
      const char& operator[](size_type pos) const;
      char& at(size_type pos);
      const char& at(size_type pos) const;

      Append Functions and Operators

      string& append(const string& s2);
      string& append(const string& s2, size_type pos2, size_type len2);
      string& append(const char* nts);
      string& append(const char* buf, size_type buflen);
      string& append(size_type repetitions, char c);

      string& operator+=(const string& s2);
      string& operator+=(const char* nts);
      string& operator+=(char c);

      Insert Functions

      string& insert(size_type pos1, const string& s2);
      string& insert(size_type pos1, const string& s2,
      size_type pos2, size_type len2);
      string& insert(size_type pos1, const char* nts);
      string& insert(size_type pos1, const char* buf, size_type buflen);
      string& insert(size_type pos1, size_type repetitions, char c);

      iterator insert(iterator pos1, char c);
      void insert(iterator pos1, size_type repetitions, char c);

      Erase Functions

      string& erase(size_type pos = 0, size_type len = npos);
      iterator& erase(iterator pos);
      iterator& erase(iterator start, iterator finish);

      Replace Functions

      string& replace(size_type pos1, size_type len1, const string& s2);
      string& replace(size_type pos1, size_type len1, const string& s2,
      size_type pos2, size_type len2);
      string& replace(size_type pos1, size_type len1, const char* nts);
      string& replace(size_type pos1, size_type len1,
      const char* buf, size_type buflen);
      string& replace(size_type pos1, size_type len1,
      size_type repetitions, char c);

      string& replace(iterator start, iterator finish, const string& s2);
      string& replace(iterator start, iterator finish, const char* nts);
      string& replace(iterator start, iterator finish,
      const char* buf, size_type buflen);
      string& replace(iterator start, iterator finish,
      size_type repetitions, char c);

      Comparison Functions

      // compare function returns -1 if *this < s2, 0 if *this == s2
      // and +1 if *this > s2.
      int compare(const string& s2) const;
      int compare(size_type pos1, size_type len1, const string& s2);
      int compare(size_type pos1, size_type len1, const string& s2,
      size_type pos2, size_type len2);
      int compare(const char* nts);
      int compare(size_type pos1, size_type len1, const char* nts);
      int compare(size_type pos1, size_type len1,
      const char* buf, size_type buflen);

      Substrings

      string substr(pos = 0, len = npos) const;

      // This constructor is repeated here because it creates substring
      string(const string& s2, size_type pos2, size_type len2);

      Conversions

      // This constructor is repeated here because it converts
      // a C string to a C++ string.
      string(const char* nts);

      const char* c_str() const; // Returns a null-terminated string
      const char* data() const; // Not null-terminated

      // Copy characters to an array. Does not append a null-terminator
      size_type copy(char* buf, size_type bufsize, size_type pos1 = 0) const;

      Search Functions

      size_type find(const string& s2, size_type pos1);
      size_type find(const char* nts, size_type pos1);
      size_type find(const char* buf, size_type pos1, size_type bufsize);
      size_type find(char c, size_type pos1);

      size_type rfind(const string& s2, size_type pos1);
      size_type rfind(const char* nts, size_type pos1);
      size_type rfind(const char* buf, size_type pos1, size_type bufsize);
      size_type rfind(char c, size_type pos1);

      size_type find_first_of(const string& s2, size_type pos1);
      size_type find_first_of(const char* nts, size_type pos1);
      size_type find_first_of(const char* buf, size_type pos1, size_type bufsize);
      size_type find_first_of(char c, size_type pos1);

      size_type find_last_of(const string& s2, size_type pos1);
      size_type find_last_of(const char* nts, size_type pos1);
      size_type find_last_of(const char* buf, size_type pos1, size_type bufsize);
      size_type find_last_of(char c, size_type pos1);

      size_type find_first_not_of(const string& s2, size_type pos1);
      size_type find_first_not_of(const char* nts, size_type pos1);
      size_type find_first_not_of(const char* buf, size_type pos1,
      size_type bufsize);
      size_type find_first_not_of(char c, size_type pos1);

      size_type find_last_not_of(const string& s2, size_type pos1);
      size_type find_last_not_of(const char* nts, size_type pos1);
      size_type find_last_not_of(const char* buf, size_type pos1,
      size_type bufsize);
      size_type find_last_not_of(char c, size_type pos1);

      String-Related Global Operators and Functions
      The following functions and operators are related to the string class but are not members of string. Along with the string class, the following definitions are in the <string> header:

      // String concatenation:
      string operator+(const string& s1, const string& s2);
      string operator+(const char* nts, const string& s2);
      string operator+(char c, const string& s2);
      string operator+(const string& s1, const char* nts);
      string operator+(const string& s1, char c);

      // Equal
      bool operator==(const string& s1, const string& s2);
      bool operator==(const char* nts, const string& s2);
      bool operator==(const string& s1, const char* nts);

      // Not-equal
      bool operator!=(const string& s1, const string& s2);
      bool operator!=(const char* nts, const string& s2);
      bool operator!=(const string& s1, const char* nts);

      // Ordering:
      bool operator<(const string& s1, const string& s2);
      bool operator<(const char* nts, const string& s2);
      bool operator<(const string& s1, const char* nts);

      // Greater than:
      bool operator>(const string& s1, const string& s2);
      bool operator>(const char* nts, const string& s2);
      bool operator>(const string& s1, const char* nts);

      // Less-than or equal-to:
      bool operator<=(const string& s1, const string& s2);
      bool operator<=(const char* nts, const string& s2);
      bool operator<=(const string& s1, const char* nts);

      // Greater-than or equal-to:
      bool operator>=(const string& s1, const string& s2);
      bool operator>=(const char* nts, const string& s2);
      bool operator>=(const string& s1, const char* nts);

      // Input and output
      ostream& operator<<(ostream& os, const string& s);
      istream& operator>>(istream& is, string& s);
      istream& getline(istream& is, string& s, char delimiter = '\n');更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • std::string or using namespace std; #include <iostream>