Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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++ 如何在while或for循环中使用.find(),并且每次都没有给出相同的find值_C++ - Fatal编程技术网

C++ 如何在while或for循环中使用.find(),并且每次都没有给出相同的find值

C++ 如何在while或for循环中使用.find(),并且每次都没有给出相同的find值,c++,C++,我试图构建一个函数,它经过一段时间或for循环,找到空间所在的位置,输出空间之前的所有内容,然后删除空间之前的所有内容,包括空间,然后再次重复 非常感谢您的帮助 int sentence() { string enteredSentence=""; getline(cin,enteredSentence); string sentenceString(enteredSentence); int sentenceLength=enteredSentence.si

我试图构建一个函数,它经过一段时间或for循环,找到空间所在的位置,输出空间之前的所有内容,然后删除空间之前的所有内容,包括空间,然后再次重复

非常感谢您的帮助

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;
    }
int语句()
{
字符串输入的句子=”;
getline(cin,输入句子);
字符串句子字符串(输入句子);
int sentenceLength=输入的句子.size();

cout我不能100%确定我是否理解您想要实现的目标。但我可以帮助您找到:

它还有第二个参数,指定搜索从字符串中的哪个位置开始:

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

std::cout我不能100%确定我是否理解您想要实现的目标。但我可以帮助您找到:

它还有第二个参数,指定搜索从字符串中的哪个位置开始:

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

std::cout我不能100%确定我是否理解您想要实现的目标。但我可以帮助您找到:

它还有第二个参数,指定搜索从字符串中的哪个位置开始:

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

std::cout我不能100%确定我是否理解您想要实现的目标。但我可以帮助您找到:

它还有第二个参数,指定搜索从字符串中的哪个位置开始:

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

std::cout这就是我修复代码的方式:

#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;
}
#包括
使用名称空间std;
int main()
{
字符串输入的句子=”;
getline(cin,输入句子);
字符串句子字符串(输入句子);
int sentenceLength=输入的句子.size();

cout这就是我修复代码的方式:

#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;
}
#包括
使用名称空间std;
int main()
{
字符串输入的句子=”;
getline(cin,输入句子);
字符串句子字符串(输入句子);
int sentenceLength=输入的句子.size();

cout这就是我修复代码的方式:

#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;
}
#包括
使用名称空间std;
int main()
{
字符串输入的句子=”;
getline(cin,输入句子);
字符串句子字符串(输入句子);
int sentenceLength=输入的句子.size();

cout这就是我修复代码的方式:

#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;
}
#包括
使用名称空间std;
int main()
{
字符串输入的句子=”;
getline(cin,输入句子);
字符串句子字符串(输入句子);
int sentenceLength=输入的句子.size();

您可以使用stringstream

#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;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
string Entered句子;//顺便说一下,默认情况下它初始化为“”
getline(cin,输入句子);

您可以使用stringstream

#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;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
string Entered句子;//顺便说一下,默认情况下它初始化为“”
getline(cin,输入句子);

您可以使用stringstream

#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;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
string Entered句子;//顺便说一下,默认情况下它初始化为“”
getline(cin,输入句子);

您可以使用stringstream

#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;
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
string Entered句子;//顺便说一下,默认情况下它初始化为“”
getline(cin,输入句子);

您的问题具体是什么?问题是当运行“enteredstation.find(“”)”时,它在查找空格时返回相同的值。是否可以,find()只找一次。我如何做每次都给出一个准确的数字。第一个数字是准确的,但是后面的数字是相同的。这里是输出注意它是a 3每次比萨饼是我最喜欢的食物句子的大小25 5比萨饼串在串之前eraced比萨饼是我最喜欢的食物串在串之后eraced是我最喜欢的e食物串大小是20 5米串之前串eraced是我最喜欢的食物串eraced最喜欢的食物串大小是15 5y fav串之前串eraced最喜欢的食物串之后串eracedorite食物串大小是10 5orite等@ike检查我的答案:)你的具体问题是什么ly?问题是当运行“enteredstation.find(“”)”时,它在查找空格时返回相同的值。是否可以,find()只找一次。我如何做每次都给出一个准确的数字。第一个数字是准确的,但是后面的数字是相同的。这里是输出注意它是a 3每次比萨饼是我最喜欢的食物句子的大小25 5比萨饼串在串之前eraced比萨饼是我最喜欢的食物串在串之后eraced是我最喜欢的e食物串大小是20 5米串之前串eraced是我最喜欢的食物串eraced最喜欢的食物串大小是15 5y fav串之前串eraced最喜欢的食物串之后串eracedorite食物串大小是10 5orite等@ike检查我的答案:)你的具体问题是什么ly?问题是当运行“enteredstation.find(“”)”时,它在查找空格时返回相同的值。是否可以,find()只找一次。我如何做每次都给出一个准确的数字。第一个数字是准确的,但是后面的数字是相同的。这里是输出注意它是a 3每次比萨饼是我最喜欢的食物句子的大小25 5比萨饼串在串之前eraced比萨饼是我最喜欢的食物串在串之后eraced是我最喜欢的食物串的大小是20 5米串在串之前是我最喜欢的食物串在串之后是我最喜欢的食物串