我將如何去扭轉字符串中的單詞順序?我試過,但它不工作:如何反轉字符串中的單詞順序(不是字母)?
string sen = "Go over there";
reverse(sen.begin(), sen.end());
但這種反轉整個字符串,但不保留詞語的正確的順序。我如何才能顛倒字符串中的單詞順序?
我將如何去扭轉字符串中的單詞順序?我試過,但它不工作:如何反轉字符串中的單詞順序(不是字母)?
string sen = "Go over there";
reverse(sen.begin(), sen.end());
但這種反轉整個字符串,但不保留詞語的正確的順序。我如何才能顛倒字符串中的單詞順序?
如果字符串中的單詞用空格分開,你可以使用一個string.find()
循環while
裏面找到斷裂,然後用string.substr()
輸出的話到vector
。然後你可以簡單地向後閱讀vector
。
如果使用C++ 11,sen.pop_back()
擺脫過去的空間,否則,你可以爲其他的例子退房Remove last character from C++ string。其次,我們不使用std::reverse(output.begin(), output.end())
,而是使用反向迭代器rbegin()
和rend()
。 for循環可以明顯改進,但它可以完成這項工作。
#include <sstream>
#include <iterator>
#include <algorithm>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
std::vector<std::string> output;
std::string sen = "Go over there";
std::string word = "";
unsigned int len = 0;
for (const auto& c : sen) {
++len;
if (c == ' ' || len == sen.size()) {
if (len == sen.size())
word += c;
output.push_back(word);
word = "";
}
if (c != ' ')
word += c;
}
std::ostringstream oss;
std::copy(output.rbegin(), output.rend(), std::ostream_iterator<std::string>(oss, " "));
sen = oss.str();
sen.pop_back(); // Get rid of last space, C++11 only
std::cout << sen;
}
我已經寫了很多這樣的字符串函數之前:
// Make copy of this original if you don't wish to destroy in the process
string sen = "Go over there";
// string that will become your reversed string
string newStr = new string();
// A temp variable that will hold the current position of the last separator character
int aChar = -1;
////////////////////////////////////////////////////////////////////////////////////////
// You may want to delete pre and post spaces here
////////////////////////////////////////////////////////////////////////////////////////
// Loop through the entire string until the original is empty
while(sen.length > 0){
// Find the last separator character (in your case a space) within the string
aChar = sen.find_last_of(" ");
// Append the word created from one char forward of the last found separator char
// to the end of the CURRENT version of the original string
newStr += sen.substr(aChar + 1, sen.length - aChar - 1);
// Store a new version of the original string that is the SUBSTRING from beginning (char 0)
// to one char before the last found separator character
sen = sen.substr(0, aChar - 1);
// Need to add the space between the words, but only if the new substring is not empty
if(sen.length > 0) newStr += " ";
}
我還沒有測試此代碼,但如果API的工作,他們的目的的方式,算法這應該工作。
作爲一個API,這可能看起來像如下
string reverse(string inStr){
// Make copy of the original so we don't destroy it in the process
string sen = inStr.copy();
// string that will become your reversed string
string newStr();
// A temp variable that will hold the current position of the last separator character
int aChar = -1;
////////////////////////////////////////////////////////////////////////////////////////
// You may want to delete pre and post spaces here
////////////////////////////////////////////////////////////////////////////////////////
// Loop through the entire string until the original is empty
while(sen.length > 0){
// Find the last separator character (in your case a space) within the string
aChar = sen.find_last_of(" ");
// Append the word created from one char forward of the last found separator char
// to the end of the CURRENT version of the original string
newStr += sen.substr(aChar + 1, sen.length - aChar - 1);
// Store a new version of the original string that is the SUBSTRING from beginning
// (char 0) to one char before the last found separator character
sen = sen.substr(0, aChar - 1);
// Need to add the space between the words, but only if the new substring is not empty
if(sen.length > 0) newStr += " ";
}
return newStr;
}
int main(int argc, char *argv[]){
string sen = "Go over there";
string rev = reverse(sen);
}
'新'?這是Java嗎? – Yakk
@Yakk - 很好的接收,謝謝:-)我上面說過我的C++是如何生鏽的LOL – trumpetlicks
你應該扭轉一次每個單詞用空格分開。 – herohuyongtao
到目前爲止你做了什麼? –
提示:將單詞分解爲矢量,然後按需打印任何訂單! – billz