1、题目
2、解决方案
1 | class Solution { |
思路很简单,使用了C 11的特性,可以十分简洁的对string进行遍历;
需要注意的是" "
表示的是 string
而' '
表示的是str
也就是说%20这个string包含了3个str,所以这里使用 +=
直接添加。
3、讲讲string
3.1 Overview and feature
字符串是代表字符序列的对象;
在
char_traits
中字符类型包括主要有以下几类:string类是basic_string类模板的实例化,该模板使用char(即字节 1byte)作为其字符类型;
需要注意的是,string类对字节的处理独立于编码的形式,例如utf-8(有1个字节,有两个字节的(大多数中文),也有三个字节的(少部分其他国家字符))举个栗子:
1 | std::string s = "abcd你好"; |
3.2、 Member functions
-
Construct string object (public member function )
-
String destructor (public member function )
-
String assignment (public member function )
Iterators:
-
Return iterator to beginning (public member function )
-
Return iterator to end (public member function )
-
Return reverse iterator to reverse beginning (public member function )
-
Return reverse iterator to reverse end (public member function )
-
Return const_iterator to beginning (public member function )
-
Return const_iterator to end (public member function )
-
Return const_reverse_iterator to reverse beginning (public member function )
-
Return const_reverse_iterator to reverse end (public member function )
Capacity:
-
Return length of string (public member function )
-
Return length of string (public member function )
-
Return maximum size of string (public member function )
-
Resize string (public member function )
-
Return size of allocated storage (public member function )
-
Request a change in capacity (public member function )
-
Clear string (public member function )
-
Test if string is empty (public member function )
-
Shrink to fit (public member function )
Element access:
-
Get character of string (public member function )
-
Get character in string (public member function )
-
Access last character (public member function )
-
Access first character (public member function )
Modifiers:
-
Append to string (public member function )
-
Append to string (public member function )
-
Append character to string (public member function )
-
Assign content to string (public member function )
-
Insert into string (public member function )
-
Erase characters from string (public member function )
-
Replace portion of string (public member function )
-
Swap string values (public member function )
-
Delete last character (public member function )
String operations:
-
Get C string equivalent (public member function )
-
Get string data (public member function )
-
Get allocator (public member function )
-
Copy sequence of characters from string (public member function )
-
Find content in string (public member function )
-
Find last occurrence of content in string (public member function )
-
Find character in string (public member function )
-
Find character in string from the end (public member function )
-
Find absence of character in string (public member function )
-
Find non-matching character in string from the end (public member function )
-
Generate substring (public member function )
-
Compare strings (public member function )
Member constants
-
Maximum value for size_t (public static member constant )
Non-member function overloads
-
Concatenate strings (function )
-
Relational operators for string (function )
-
Exchanges the values of two strings (function )
-
Extract string from stream (function )
-
Insert string into stream (function )
-
Get line from stream into string (function )