Cao Lin's Blog.

CPP string 简介

Word count: 732Reading time: 3 min
2020/04/06 Share

1、题目

image-20200406094953830

2、解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
string replaceSpace(string s) {
string temp_s;
for (auto str:s)
{
if (str == ' ') {temp_s += "%20";}
else {temp_s += str;}
}
return temp_s;
}
};
/*
执行用时 :4 ms, 在所有 C++ 提交中击败了55.34%的用户
内存消耗 :6.2 MB, 在所有 C++ 提交中击败了100.00%的用户
*/

思路很简单,使用了C 11的特性,可以十分简洁的对string进行遍历;

需要注意的是" "表示的是 string' '表示的是str也就是说%20这个string包含了3个str,所以这里使用 +=直接添加。

3、讲讲string

3.1 Overview and feature

c plus plus.com

  • 字符串是代表字符序列的对象;

  • char_traits中字符类型包括主要有以下几类:

    image-20200406100931327

  • string类是basic_string类模板的实例化,该模板使用char(即字节 1byte)作为其字符类型;

  • 需要注意的是,string类对字节的处理独立于编码的形式,例如utf-8(有1个字节,有两个字节的(大多数中文),也有三个字节的(少部分其他国家字符))举个栗子:

1
2
3
std::string s = "abcd你好";
// 长度是8个字节
std::cout << s.length() << std::endl;
3.2、 Member functions
  • (constructor)

    Construct string object (public member function )

  • (destructor)

    String destructor (public member function )

  • operator=

    String assignment (public member function )

Iterators:

  • begin

    Return iterator to beginning (public member function )

  • end

    Return iterator to end (public member function )

  • rbegin

    Return reverse iterator to reverse beginning (public member function )

  • rend

    Return reverse iterator to reverse end (public member function )

  • cbegin

    Return const_iterator to beginning (public member function )

  • cend

    Return const_iterator to end (public member function )

  • crbegin

    Return const_reverse_iterator to reverse beginning (public member function )

  • crend

    Return const_reverse_iterator to reverse end (public member function )

Capacity:

  • size

    Return length of string (public member function )

  • length

    Return length of string (public member function )

  • max_size

    Return maximum size of string (public member function )

  • resize

    Resize string (public member function )

  • capacity

    Return size of allocated storage (public member function )

  • reserve

    Request a change in capacity (public member function )

  • clear

    Clear string (public member function )

  • empty

    Test if string is empty (public member function )

  • shrink_to_fit

    Shrink to fit (public member function )

Element access:

  • operator

    Get character of string (public member function )

  • at

    Get character in string (public member function )

  • back

    Access last character (public member function )

  • front

    Access first character (public member function )

Modifiers:

  • operator+=

    Append to string (public member function )

  • append

    Append to string (public member function )

  • push_back

    Append character to string (public member function )

  • assign

    Assign content to string (public member function )

  • insert

    Insert into string (public member function )

  • erase

    Erase characters from string (public member function )

  • replace

    Replace portion of string (public member function )

  • swap

    Swap string values (public member function )

  • pop_back

    Delete last character (public member function )

String operations:

  • c_str

    Get C string equivalent (public member function )

  • data

    Get string data (public member function )

  • get_allocator

    Get allocator (public member function )

  • copy

    Copy sequence of characters from string (public member function )

  • find

    Find content in string (public member function )

  • rfind

    Find last occurrence of content in string (public member function )

  • find_first_of

    Find character in string (public member function )

  • find_last_of

    Find character in string from the end (public member function )

  • find_first_not_of

    Find absence of character in string (public member function )

  • find_last_not_of

    Find non-matching character in string from the end (public member function )

  • substr

    Generate substring (public member function )

  • compare

    Compare strings (public member function )

Member constants

  • npos

    Maximum value for size_t (public static member constant )

Non-member function overloads

  • operator+

    Concatenate strings (function )

  • relational operators

    Relational operators for string (function )

  • swap

    Exchanges the values of two strings (function )

  • operator>>

    Extract string from stream (function )

  • operator<<

    Insert string into stream (function )

  • getline

    Get line from stream into string (function )

CATALOG
  1. 1. 1、题目
  2. 2. 2、解决方案
  3. 3. 3、讲讲string
    1. 3.1. 3.1 Overview and feature
    2. 3.2. 3.2、 Member functions
  • Member constants
  • Non-member function overloads