目录
151. Reverse Words in a String
-
string中的 reserve 与 resize:
- reserve 为容器预留足够的空间,避免不必要的重复分配,分配空间大于等于函数的参数,影响capacity。
- resize 调整容器中有效数据区域的尺寸,如果尺寸变小,原来数据多余的截掉,若尺寸变大,不够的数据用该函数的第二个参数填充,影响size和capacity。
- string str = "hello";
- string str = "hello";str.reserve(20);
- 经过reserve,保留空间变大了,内存字节对齐,n*16-1
- string str = "hello";str.resize(20);
- string str = "hello";str.resize(2);
-
istringstream用法
- C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含<sstream>这个头文件。
- istringstream:用于执行C++风格的串流的输入,从string对象str中读取字符
- ostringstream:用于执行C风格的串流的输出
- stringstream:可以同时支持C风格的串流的输入输出
#include"pch.h"
#include<iostream>
#include<vector>
#include<sstream>
#include<string>
using namespace std;
int main() {
string str = "hello world";
istringstream is(str);
string s;
while (is >> s) {
cout << s << ",";
}
cout << endl;
return 0;
}
-
stringstream用法
- 如果想用sprintf函数将一个变量从int转化为字符串类型,必须确保目标缓冲区有足够大的空间来容纳转换完的字符串,另外,必须使用正确的格式化符,如果使用了不正确的格式化符,回导致非预知的后果。
- 例如:格式化符使用错误。int n=10000;chars[10];sprintf(s,”%d”,n);// s中的内容为“10000”;变成:int n=10000;chars[10];sprintf(s,”%f”,n);
- 因为s和n的类型在编译时已经知道,所以编译器拥有足够的信息来判断需要哪些转换,<sstream>中声明的标准类可以自动选择必须的转换,转换结果保存在stringstream对象的内部缓存中,不用担心缓存移除,因为这些对象会根据需要自动分配空间。
- <sstream>使用string对象来代替字符数组,可以避免缓冲区溢出的风险,并且传入参数和目标对象的类型被自动推导。
- 多次使用同一个stringstream,需要clear,多次转换中使用同一个stringstream对象,可效率高,不用每次都构造和析构。
- 例如,string到int的转换:
#include"pch.h"
#include<iostream>
#include<vector>
#include<sstream>
#include<string>
using namespace std;
int main() {
string str1 = "100000";
string str2 = "200000";
int n = 0;
stringstream stream;
/*1.clear后重新赋值,n分别等于100000和200000
2.clear后不赋值,n分别等于100000和100000
3.不clear,n分别等于100000和100000*/
stream << str1;
stream >> n;
cout << "1: "<<n << endl;
stream.clear();
stream << str2;
stream >> n;
cout << "2: " << n << endl;
return 0;
}
151. Reverse Words in a String
Given an input string, reverse the string word by word.
Example 1:
Input: "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: " hello world! "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
Note:
- A word is defined as a sequence of non-space characters.
- Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
- You need to reduce multiple spaces between two words to a single space in the reversed string.
Follow up:
For C programmers, try to solve it in-place in O(1) extra space.
#include"pch.h"
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<algorithm>
/*151. Reverse Words in a String*/
using namespace std;
/*1.
执行用时 :28 ms, 在所有 C++ 提交中击败了14.74%的用户
内存消耗 :120.1 MB, 在所有 C++ 提交中击败了23.61%的用户*/
string reverseWords1(string s) {
string res,tmp;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ' && tmp!="") {
if (res != "") res = " "+res;
res = tmp+res;
tmp = "";
}
else if(s[i]!=' ') tmp += s[i];
}
if (tmp != "") {
if (res != "") res = " " + res;
res = tmp + res;
}
return res;
}
/*2.空间复杂度O(1),只能对原字符串做修改,先翻转整个字符串,再分别翻转每个单词。
执行用时 :12 ms, 在所有 C++ 提交中击败了57.06%的用户
内存消耗 :7.5 MB, 在所有 C++ 提交中击败了100.00%的用户*/
string reverseWords2(string s) {
reverse(s.begin(), s.end());
int newlen = 0;//新的长度,要去掉原来的多余的空格
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') {//如果不是空格
if (newlen != 0) s[newlen++] = ' ';
//如果不是第一个单词,在原先的基础上加个空格
int j = i;//从这个地方开始是一个新的单词,翻转这个单词
while (j < s.size() && s[j] != ' ') s[newlen++] = s[j++];//找到这个单词
reverse(s.begin() + newlen - (j - i), s.begin() + newlen);
//翻转这个单词,这个单词的长度是j-i,开始的地方是newlen-(j-i)
i = j;//更新
}
}
s.resize(newlen);//截断
return s;
}
/*3.字符串流类stringstream的解法,将字符串装入字符串流中,然后定义一个临时变量tmp,将第一个单词给s,
如果含有非空格字符,每次>>操作就会提取连在一起的非空格字符,那么每次将它加在s前面就可以,
如果原字符是空,就不会进入while循环,
如果原字符为许多空格连在一起,那么第一个>>操作就会提取出这些空格放入s,不会进入while,此时需要判断s的首字符是否为空,是就将s清空
需要包含头文件:<sstream>
*/
void reverseWords(string &s) {
istringstream is(s);
string tmp;
is >> s;//提取连在一起的非空字符,放入s
while (is >> tmp) s = tmp + " " + s;
if (!s.empty() && s[0] == ' ') s = "";
}
int main() {
string s = " hello world! ";
reverseWords(s);
cout << s;
return 0;
}