📄 string.h
字号:
#endif
// Copy up to n characters from this string into s
size_type copy(char* s, size_type n, size_type pos = 0);
// Exchange the values of two strings.
void swap(string&);
// string operations:
// Return a C-style string equivalent to this one. Program must not
// alter this string in any way.
const char* c_str() const; // explicit
const char* data() const; // explicit
// returns c_str() is size()>0, else returns some non-null pointer
// search for a string within this one, returning position where it is
// found (or npos if it is not found)
size_type find (const string& str, size_type pos = 0) const;
size_type find (const char* s, size_type pos, size_type n) const;
size_type find (const char* s, size_type pos = 0) const;
size_type find (char c, size_type pos = 0) const;
// Same as find, but start search from high end of string
size_type rfind(const string& str, size_type pos = npos) const;
size_type rfind(const char* s, size_type pos, size_type n) const;
size_type rfind(const char* s, size_type pos = npos) const;
size_type rfind(char c, size_type pos = npos) const;
// Find lowest position in which ANY char of str appears within this
// string. Returns npos if no such position exists.
size_type find_first_of(const string& str, size_type pos = 0) const;
size_type find_first_of(const char* s, size_type pos, size_type n) const;
size_type find_first_of(const char* s, size_type pos = 0) const;
size_type find_first_of(char c, size_type pos = 0) const;
// Find highest position in which ANY char of str appears within this
// string. Returns npos if no such position exists.
size_type find_last_of (const string& str,
size_type pos = npos) const;
size_type find_last_of (const char* s, size_type pos, size_type n) const;
size_type find_last_of (const char* s, size_type pos = npos) const;
size_type find_last_of (char c, size_type pos = npos) const;
// Find lowest position in which NO char of str appears within this
// string. Returns npos if no such position exists.
size_type find_first_not_of(const string& str,
size_type pos = 0) const;
size_type find_first_not_of(const char* s, size_type pos,
size_type n) const;
size_type find_first_not_of(const char* s, size_type pos = 0) const;
size_type find_first_not_of(char c, size_type pos = 0) const;
// Find highest position in which NO char of str appears within this
// string. Returns npos if no such position exists.
size_type find_last_not_of (const string& str,
size_type pos = npos) const;
size_type find_last_not_of (const char* s, size_type pos,
size_type n) const;
size_type find_last_not_of (const char* s, size_type pos = npos) const;
size_type find_last_not_of (char c, size_type pos = npos) const;
// Extract a substring.
string substr(size_type pos = 0, size_type n = npos) const;
//pre: pos >= 0 && pos <= size()
// s2 = substr(s1, pos, n);
//post: s2.size() == min(n, s1.size()-pos)
// for all i in 0..s2.size(), s2[i] == s1[i+pos]
// Comparison operators. Returns
// 1 if this string > str
// 0 if this string == str
// -1 if this string < str
int compare(const string& str) const;
int compare(size_type pos1, size_type n1, const string& str) const;
int compare(size_type pos1, size_type n1, const string& str,
size_type pos2, size_type n2) const;
int compare(const char* s) const;
int compare(size_type pos1, size_type n1, const char* s,
size_type n2 = npos) const;
private:
size_type len;
size_type capacty;
size_type* refcnt;
char* chars;
void rep_alloc (size_type requested);
static void rep_free (size_type*&, size_type);
void rep_append(const char* from, size_type n);
void rep_insert(const char* from, size_type n, size_type at);
void rep_erase(size_type at, size_type n);
size_type rep_find (size_type start,
const char* s, size_type n) const;
size_type rep_rfind (size_type start, const char* s, size_type n) const;
size_type rep_scan (size_type start, const bool* s) const;
size_type rep_rscan (size_type start, const bool* s) const;
void uncheckedAssign(const char* s, size_type n);
void getUniqueBlock(size_type cap, bool preserveValue);
void removeReference(size_type* &rep, size_type capac);
char* nonConstOp();
static void markCharSet (bool* set, const char* s,
size_type n, bool value);
static size_type emptyStringArea[2];
static char* const emptystr;
static void allocate (size_type numChars,
size_type* &ptr, size_type &numAllocated);
static void release (size_type* ptr, size_type numChars);
#ifdef _MEMDEBUG
int id;
static int nextID;
public:
enum MaxID {maxID = 1000};
private:
static size_type* freed[16];
static string* allStrings[maxID];
static const size_type barrierValue;
public:
bool OK() const; // checks for internal consistency
static int allStringsOK(); // 0 if all OK, -s.id if s.OK() fails
static bool checkFreeLists();
#endif
};
istream& operator>> (istream &, string &);
ostream& operator<< (ostream &, const string &);
istream& getline (istream& is, string& str, char delim='\n');
inline string& string::operator=(const string& str)
{
return assign(str, 0, npos);
}
inline string& string::operator=(char c)
{
return assign(1, c);
}
inline string& string::assign(const char* s)
{
return assign(s, npos);
}
inline string& string::operator=(const char* s)
{
return assign(s);
}
inline string::iterator string::begin() {return nonConstOp();}
inline string::const_iterator string::begin() const {return chars;}
inline string::iterator string::end() {return nonConstOp() + len;}
inline string::const_iterator string::end() const {return chars + len;}
#ifndef NO_REVERSE_ITERATORS
inline string::reverse_iterator string::rbegin() {return end();}
inline string::const_reverse_iterator string::rbegin() const {return end();}
inline string::reverse_iterator string::rend() {return begin();}
inline string::const_reverse_iterator string::rend() const {return begin();}
#endif
inline string::size_type string::size() const
{return len;}
inline string::size_type string::length() const
{return len;}
inline bool string::empty() const
{return (len == 0);}
inline char string::operator[](size_type pos) const
{
assert (pos <= length());
return chars[pos];
}
inline char& string::operator[](size_type pos)
{
assert (pos <= length());
return nonConstOp()[pos];
}
inline string::const_reference string::at(size_type pos) const
{
assert (pos < length());
return chars[pos];
}
inline char& string::at(size_type pos)
{
assert (pos < length());
return nonConstOp()[pos];
}
inline const char* string::
c_str() const // explicit
{
return chars;
}
inline string& string::insert(size_type pos1, const string& str)
{return insert(pos1, str, 0, npos);}
inline string& string::replace(iterator first, iterator last, const char* s)
{ return replace (first, last, s, npos); }
inline string& string::erase(iterator position)
{return erase (position, position+1);}
inline string string::
substr(size_type pos, size_type n) const
{
return string().append(*this, pos, n);
}
inline void string::resize(size_type n) {resize(n, 0);}
inline string::size_type string::capacity() const {return capacty;}
inline string& string::append(const char* s) {return append(s, npos);}
inline string& string::operator+=(const string& rhs) {return append(rhs);}
inline string& string::operator+=(const char* s) {return append(s);}
inline string& string::operator+=(char c) {return append(1, c);}
inline string operator+ (const string& lhs, const string& rhs)
{return string(lhs).append(rhs);}
inline string operator+ (const char* lhs, const string& rhs)
{return string(lhs).append(rhs);}
inline string operator+ (char lhs, const string& rhs)
{return string(1, lhs).append(rhs);}
inline string operator+ (const string& lhs, const char* rhs)
{return string(lhs).append(rhs);}
inline string operator+ (const string& lhs, char rhs)
{return string(lhs).append(1,rhs);}
inline bool operator== (const string& lhs, const string& rhs)
{return lhs.compare(rhs) == 0;}
inline bool operator== (const string& lhs, const char* rhs)
{return lhs.compare(rhs) == 0;}
inline bool operator== (const char* lhs, const string& rhs)
{return rhs.compare(lhs) == 0;}
inline bool operator!= (const string& lhs, const string& rhs)
{return lhs.compare(rhs) != 0;}
inline bool operator!= (const string& lhs, const char* rhs)
{return lhs.compare(rhs) != 0;}
inline bool operator!= (const char* lhs, const string& rhs)
{return rhs.compare(lhs) != 0;}
inline bool operator< (const string& lhs, const string& rhs)
{return lhs.compare(rhs) < 0;}
inline bool operator< (const string& lhs, const char* rhs)
{return lhs.compare(rhs) < 0;}
inline bool operator< (const char* lhs, const string& rhs)
{return rhs.compare(lhs) > 0;}
inline bool operator<= (const string& lhs, const string& rhs)
{return lhs.compare(rhs) <= 0;}
inline bool operator<= (const string& lhs, const char* rhs)
{return lhs.compare(rhs) <= 0;}
inline bool operator<= (const char* lhs, const string& rhs)
{return rhs.compare(lhs) >= 0;}
inline bool operator> (const string& lhs, const string& rhs)
{return lhs.compare(rhs) > 0;}
inline bool operator> (const string& lhs, const char* rhs)
{return lhs.compare(rhs) > 0;}
inline bool operator> (const char* lhs, const string& rhs)
{return rhs.compare(lhs) < 0;}
inline bool operator>= (const string& lhs, const string& rhs)
{return lhs.compare(rhs) >= 0;}
inline bool operator>= (const string& lhs, const char* rhs)
{return lhs.compare(rhs) >= 0;}
inline bool operator>= (const char* lhs, const string& rhs)
{return rhs.compare(lhs) <= 0;}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -