0

私は文字列を持っています

string str= "Jhon 12345 R333445 3434";
string str1= "Mike 00987 #F54543";    

したがって、str から私が欲しい"R333445 3434"のは、2 番目のスペース文字の後に表示されるものが何であれ、同様にすべてが必要なためです。str1 "#F54543"

stringstream を使用して、スペースの後の次の単語を抽出しましたが、正しい結果が得られません

str ="Jhon 12345 R333445 3434";

それはR333445を与えるべきです"R333445 3434"

私の問題のより良いロジックを提案してください。

4

3 に答える 3

2

どうですか

#include <string>
#include <iostream>

int main()
{
  const std::string str = "Jhon 12345 R333445 3434";

  size_t pos = str.find(" ");
  if (pos == std::string::npos)
    return -1;

  pos = str.find(" ", pos + 1);
  if (pos == std::string::npos)
    return -1;

  std::cout << str.substr(pos, std::string::npos);
}

出力

 R333445 3434

http://ideone.com/P1Knbeによると。

于 2013-05-12T16:33:34.927 に答える
2

最初の 2 つの単語を飛ばして残りを読みたいようですが、それが正しければ、次のようなことができます。

std::string str("Jhon 12345 R333445 3434"");
std::string tmp, rest;

std::istringstream iss(str);
// Read the first two words.    
iss >> tmp >> tmp;
// Read the rest of the line to 'rest'
std::getline(iss,rest);
std::cout << rest;
于 2013-05-12T16:35:04.483 に答える