Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在字符串C+中查找倒数第二个单词+;_C++_String_Visual Studio_Visual C++ - Fatal编程技术网

C++ 在字符串C+中查找倒数第二个单词+;

C++ 在字符串C+中查找倒数第二个单词+;,c++,string,visual-studio,visual-c++,C++,String,Visual Studio,Visual C++,您好,我需要在字符串中查找倒数第二个单词。现在下面的程序正在打印最后一个 #include <iostream> #include <string> using namespace std; int main() { string text{"some line with text"}; // find last space, counting from backwards int i = text.length() - 2

您好,我需要在
字符串中查找倒数第二个单词。现在下面的程序正在打印最后一个

#include <iostream>
#include <string>

using namespace std;

int main() {
    string text{"some line with text"};
    // find last space, counting from backwards
    int i = text.length() - 2; // last character
    while (i != 0 && !isspace(text[i]))
    {
      --i;
    }
    string lastword = text.substr(i+1); // +1 to skip leading space
    cout << lastword << endl;
    return 0;
}  

您可以将字符串拆分为单词,并在保存当前单词之前保留上一个单词

#include <iostream>
#include <string>
#include <sstream>

int main() {
    std::string text{"some line with text"};
    std::stringstream ss(text);
    std::string previousword, lastword, newword;
    while (ss >> newword) {
        previousword = lastword;
        lastword = newword;
    }
    std::cout << previousword << std::endl;
    return 0;
}
#包括
#包括
#包括
int main(){
字符串文本{“带文本的某行”};
std::stringstream ss(文本);
std::字符串上一个单词、最后一个单词、新单词;
while(ss>>新词){
前一个字=最后一个字;
lastword=新词;
}

std::cout只需继续计算空格,直到找到所需的单词或使用MikeCAT建议的stringstream

这里有一个函数可以查找任何最后一个字号,而不必复制stringstream中的整个字符串:

#包括
#包括
使用std::string;
使用std::cout;
使用std::endl;
字符串getNLastWord(字符串文本,int n)
{
bool-insideAWord=false;
int-wordNum=0;
int wordEnd=-1;
对于(int i=text.size()-1;i>0;i--)
{
if(文本[i]!=''&!insideAWord)
{
wordNum++;
单词=真;
}
else if(文本[i]='')
{
单词=假;
}
if(wordNum==n)
{
wordEnd=i;
打破
}
}
如果(wordEnd==-1)
{

cout您不需要任何循环。只需添加错误检查:

int main() {
  std::string text{ "some line with text" };
  std::size_t pos2 = text.rfind(' ');
  std::size_t pos1 = text.rfind(' ', pos2-1);

  std::cout << text.substr(pos1+1, pos2-pos1-1) << std::endl;
  return 0;
}
intmain(){
字符串文本{“带文本的某行”};
std::size\u t pos2=text.rfind(“”);
std::size\u t pos1=text.rfind(“”,pos2-1);

std::不能注意,这将遍历整个文本以找到最后一个单词。这将使它更快:
std::stringstream ss({text.rbegin(),text.rend()});
,然后只取第二个单词。请记住,在这种情况下,单词将被反转。然后反转反转单词:
std::string(secondword.rbegin()),secondword.rend())
只需获取已有的代码并继续迭代,直到找到下一个空格。
int main() {
  std::string text{ "some line with text" };
  std::size_t pos2 = text.rfind(' ');
  std::size_t pos1 = text.rfind(' ', pos2-1);

  std::cout << text.substr(pos1+1, pos2-pos1-1) << std::endl;
  return 0;
}