2015-07-04 14 views
0

我试图构建一个函数,它会经过一段时间或for循环,并找到空间的位置输出空间前的所有内容,然后在包含空间的空间之前擦除所有内容,然后再次重复此操作。如何在while或for循环中使用.find(),并且每次都没有给出相同的发现值

任何帮助,非常感谢。

int sentence() 
{ 
    string enteredSentence=""; 

    getline(cin,enteredSentence); 
    string sentenceString(enteredSentence); 

    int sentenceLength=enteredSentence.size(); 
    cout<<"size of sentence"<<sentenceLength<<endl; 
    int stringSize=sentenceString.size(); 
    while(stringSize>0) 
    { 
     int spaceLoc = enteredSentence.find(" "); 
     cout<<spaceLoc; 

     cout<<sentenceString.substr(0,spaceLoc)<<endl; 
     sentenceString.substr(0,spaceLoc); 
     cout<<"string before string eraced"<<sentenceString<<endl; 

     sentenceString.erase (0,spaceLoc); 
     cout<<"string after string eraced"<<sentenceString<<endl; 

     stringSize=sentenceString.size(); 
     cout<<"string size is"<<stringSize<<endl; 
    } 
+0

具体是什么问题? – Ediac

+0

问题是当“enteredSentence.find(”“);”运行时它会在查找空间时返回相同的值。可以的,find()只能找到一次。我如何每次发出准确的数字。第一个数字是准确的,不过数字后是相同 – ike

+0

这里输出注意到它的一个3每次 – ike

回答

1

这是我的固定代码:

#include <iostream> 

using namespace std; 

int main() 
{ 
    string enteredSentence=""; 

    getline(cin,enteredSentence); 
    string sentenceString(enteredSentence); 

    int sentenceLength = enteredSentence.size(); 
    cout<<"size of sentence:"<<sentenceLength<<endl; 
    string::size_type stringSize = sentenceString.size(); 
    while(stringSize > 0) 
    { 
     int spaceLoc = sentenceString.find(" "); //there was incorrect var 
     cout<<spaceLoc<<endl; 

     if(spaceLoc == string::npos){ 
      cout<<"last part:"<<sentenceString<<endl; 
      break; 
     }//check if there are no spaces left 

     cout<<sentenceString.substr(0,spaceLoc)<<endl; 
     //the substr line here was redundant 
     cout<<"string before string erased:"<<sentenceString<<endl; 

     sentenceString.erase(0, spaceLoc + 1);//also delete the space 
     cout<<"string after string erased:"<<sentenceString<<endl; 

     stringSize=sentenceString.size(); 
     cout<<"string size:"<<stringSize<<endl<<endl; 

    } 
    return 0; 
} 
+2

我会将'spaceLoc'的​​类型更改为'string :: size_type'并使用'if(spaceLoc == string :: npos)'。 –

+0

@RSahu谢谢,修复它。 :) – fsacer

1

我不是100%确定我明白你想达到什么目的。但是,我可以帮你找:

它有一个从字符串在对搜索将启动指定第二个参数:

size_t pos = 0; 
while ((pos = str.find(' ', pos)) != std::string::npos) { 
    std::cout << "Found a space at " << pos << std::endl; 
    ++pos; 
} 

Reference

随着你真正想要的更多信息你的代码(显示示例输入和需要的输出)我可以帮助你清除代码的其余部分。
当前您的描述建议您要输出整个字符串,但是分成部分(用空格分隔)。

您的代码会为您的输入生成一个(不需要的)副本,生成子字符串只会将它们丢弃并且不返回如函数声明中所述的int

如果你想标记你的输入,那么this question有一些答案给你。

+0

你需要在随后的迭代中增加'pos',否则每次只会继续找到相同的空格字符,因为你开始于找到第一个结果后的相同位置。搜索中包含指定的位置。 –

+0

哎呀,谢谢。编辑。 –

1

你可以使用一个字符串流。

#include <sstream> 
#include <iostream> 
using namespace std; 
int main(int argc, char* argv[]) { 
    string enteredSentence; // It's initialized to "" by default, by the way 
    getline(cin,enteredSentence); 
    cout<<"size of sentence: "<<enteredSentence.length()<<endl; 
    istringstream str_in(enteredSentence); 
    string word; 
    while(str_in >> word) { 
     // Do stuff with word 
     // I believe str_in.str() will also give you the portion that hasn't yet been processed. 
    } 
    return 0; 
} 
相关问题