std::string简介及其使用

注:std::string C++11标准。

 

string概述

typedef basic_string<char> string;

  字符串是表示字符序列的对象。
  标准string类使用类似于字节标准容器的接口提供对此类对象的支持,但是添加了专门用于操作单字节字符(single-byte characters)的字符串的特性。
  string类是basic_string类模板的实例化,该模板使用char作为其字符类型,并具有默认的char_traits和allocator类型。
  需要注意的是,这个类独立于所使用的编码来处理字节(即与编码无关):如果用于处理多字节或可变长度字符(如UTF-8)的序列,那么这个类的所有成员(如长度或大小)及其迭代器仍将以字节(而不是实际编码的字符)进行操作。

 

成员类型

member type definition
value_type char
traits_type char_traits
allocator_type allocator
reference char&
const_reference const char&
pointer char*
const_pointer const char*
iterator a random access iterator to char (convertible to const_iterator)
const_iterator a random access iterator to const char
reverse_iterator reverse_iterator
const_reverse_iterator reverse_iterator
difference_type ptrdiff_t
size_type size_t

 

成员函数

  • (constructor) 构造函数
default (1)
string();
copy (2)
string (const string& str);
substring (3)
string (const string& str, size_t pos, size_t len = npos);
from c-string (4)
string (const char* s);
from buffer (5)
string (const char* s, size_t n);
fill (6)
string (size_t n, char c);
range (7)
template 
string (InputIterator first, InputIterator last);
initializer list (8)
string (initializer_list il);
move (9)
string (string&& str) noexcept;

  构造函数示例:

// string constructor
#include 
#include <string>

int main ()
{
  std::string s0 ("Initial string");

  // constructors used in the same order as described above:
  std::string s1;
  std::string s2 (s0);
  std::string s3 (s0, 8, 3);
  std::string s4 ("A character sequence");
  std::string s5 ("Another character sequence", 12);
  std::string s6a (10, 'x');
  std::string s6b (10, 42);      // 42 is the ASCII code for '*'
  std::string s7 (s0.begin(), s0.begin()+7);

  std::cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3;
  std::cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6a: " << s6a;
  std::cout << "\ns6b: " << s6b << "\ns7: " << s7 << '\n';
  return 0;
}

Output:
s1: 
s2: Initial string
s3: str
s4: A character sequence
s5: Another char
s6a: xxxxxxxxxx
s6b: **********
s7: Initial
构造函数示例
  • (destructor) 析构函数
~string();
  • operator= 赋值
string (1)
string& operator= (const string& str);
c-string (2)
string& operator= (const char* s);
character (3)
string& operator= (char c);
initializer list (4)
string& operator= (initializer_list il);
move (5)
string& operator= (string&& str) noexcept;
  • 迭代器相关
函数 函数原型 功能描述
begin iterator begin() noexcept; Return iterator to   beginning 
const_iterator   begin() const noexcept;
end iterator end() noexcept; Return iterator to   end 
const_iterator   end() const noexcept;
rbegin reverse_iterator rbegin()   noexcept; Return reverse   iterator to reverse beginning 
const_reverse_iterator   rbegin() const noexcept;
rend reverse_iterator rend()   noexcept; Return reverse   iterator to reverse end 
const_reverse_iterator   rend() const noexcept;
cbegin  const_iterator cbegin() const   noexcept; Return const_iterator to   beginning 
cend  const_iterator cend() const noexcept; Return const_iterator to   end 
crbegin  const_reverse_iterator crbegin() const noexcept; Return const_reverse_iterator to   reverse beginning 
crend  const_reverse_iterator crend()   const noexcept; Return const_reverse_iterator to   reverse end 

   迭代器示例:

// string::begin/end
#include 
#include <string>

int main ()
{
  std::string str ("Test string");
  for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
    std::cout << *it;
  std::cout << '\n';

  return 0;
}

Output:
Test string
迭代器示例
  • 容器大小或容量相关
函数 函数原型 功能描述
size size_t size() const noexcept; Return length of string
length size_t length() const noexcept; Return length of string
max_size size_t max_size() const   noexcept; Return maximum size of string
resize void resize (size_t n); Resize string 
void resize (size_t n, const value_type& val);
capacity size_t capacity() const noexcept; Return size of allocated storage
reserve void reserve (size_t n = 0); Request a change in   capacity 
clear void clear() noexcept; Clear string
empty bool empty() const noexcept; Test if string is empty
shrink_to_fit  void shrink_to_fit(); Shrink to fit 

  示例:

// resizing string
#include 
#include <string>

int main ()
{
  std::string str ("I like to code in C");
  std::cout << str << '\n';

  unsigned sz = str.size();

  str.resize (sz+2,'+');
  std::cout << str << '\n';

  str.resize (14);
  std::cout << str << '\n';
  return 0;
}

Output:
I like to code in C
I like to code in C++
I like to code
----------------------------------------------------------
// comparing size, length, capacity and max_size
#include 
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "size: " << str.size() << "\n";
  std::cout << "length: " << str.length() << "\n";
  std::cout << "capacity: " << str.capacity() << "\n";
  std::cout << "max_size: " << str.max_size() << "\n";
  return 0;
}

A possible output for this program could be:
size: 11
length: 11

你可能感兴趣的:(c/c++)