Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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++;使用substr和find?_C++_String - Fatal编程技术网

C++ 如何在C++;使用substr和find?

C++ 如何在C++;使用substr和find?,c++,string,C++,String,我使用了这个函数,但它是错误的 for (int i=0; i<sen.length(); i++) { if (sen.find (' ') != string::npos) { string new = sen.substr(0,i); } cout << "Substrings:" << new << endl; } >(int i=0;i 新< /Cord>是C++中的关键字,所以第一步是不使用它作为变量名。<

我使用了这个函数,但它是错误的

for (int i=0; i<sen.length(); i++) {
    if (sen.find (' ') != string::npos) {
        string new = sen.substr(0,i);
    }
cout << "Substrings:" << new << endl;
}

<代码> >(int i=0;i <代码>新< /Cord>是C++中的关键字,所以第一步是不使用它作为变量名。< /P>

之后,需要将输出语句放入“IF”块中,以便它实际上可以被允许访问子字符串。C++中的作用域是至关重要的。

<代码>代码<新>代码>是C++中的关键字,所以第一步是不使用它作为变量名。

之后,需要将输出语句放入“IF”块中,以便它实际上可以被允许访问子字符串。C++中的作用域是至关重要的。

< P>第一:这不能编译,因为<代码>新< /Cord>是一个语言关键字。
然后在字符串中的每个字符中都有一个循环,因此不需要使用
std::string::find
。我会使用
std::string::find
,但是循环条件应该不同。

首先:这无法编译,因为
new
是一个语言关键字


然后在字符串中的每个字符中都有一个循环,因此不需要使用
std::string::find
。我会使用
std::string::find
,但是循环条件应该不同。

无需对字符串进行迭代,
find
已经完成了此操作。它通过defa从一开始搜索ult,所以一旦我们找到一个空间,我们需要从这个找到的空间开始下一次搜索:

std::vector<std::string> words;

//find first space
size_t start = 0, end = sen.find(' ');

//as long as there are spaces
while(end != std::string::npos)
{
    //get word
    words.push_back(sen.substr(start, end-start));

    //search next space (of course only after already found space)
    start = end + 1;
    end = sen.find(' ', start);
}

//last word
words.push_back(sen.substr(start));
std::向量词;
//找到第一个空间
大小\u t开始=0,结束=sen.find(“”);
//只要有空间
while(end!=std::string::npos)
{
//得到消息
文字。推回(sen.substr(开始,结束-开始));
//搜索下一个空间(当然只能在已经找到的空间之后)
开始=结束+1;
end=sen.find(“”,start);
}
//最后一句话
单词。推回(sen.substr(start));
当然,这不会处理重复空格、起始空格或尾随空格以及其他特殊情况。实际上,您最好使用stringstream:

#include <sstream>
#include <algorithm>
#include <iterator>

std::istringstream stream(sen);
std::vector<std::string> words(std::istream_iterator<std::string>(stream), 
                               std::istream_iterator<std::string>());
#包括
#包括
#包括
std::istringstream(sen);
std::矢量字(std::istream_迭代器(流),
std::istreamu迭代器();
然后,您可以随意将其输出,或者直接在循环中执行,而无需使用向量:

for(std::vector<std::string>::const_iterator iter=
    words.begin(); iter!=words.end(); ++iter)
    std::cout << "found word: " << *iter << '\n';
for(std::vector::const_迭代器iter)=
words.begin();iter!=words.end();++iter)

std::cout无需对字符串进行迭代,
find
已经执行了此操作。默认情况下,它从一开始就开始搜索,因此一旦我们找到了一个空间,我们就需要从这个找到的空间开始下一次搜索:

std::vector<std::string> words;

//find first space
size_t start = 0, end = sen.find(' ');

//as long as there are spaces
while(end != std::string::npos)
{
    //get word
    words.push_back(sen.substr(start, end-start));

    //search next space (of course only after already found space)
    start = end + 1;
    end = sen.find(' ', start);
}

//last word
words.push_back(sen.substr(start));
std::向量词;
//找到第一个空间
大小\u t开始=0,结束=sen.find(“”);
//只要有空间
while(end!=std::string::npos)
{
//得到消息
文字。推回(sen.substr(开始,结束-开始));
//搜索下一个空间(当然只能在已经找到的空间之后)
开始=结束+1;
end=sen.find(“”,start);
}
//最后一句话
单词。推回(sen.substr(start));
当然,这不会处理重复空格、起始空格或尾随空格以及其他特殊情况。实际上,您最好使用stringstream:

#include <sstream>
#include <algorithm>
#include <iterator>

std::istringstream stream(sen);
std::vector<std::string> words(std::istream_iterator<std::string>(stream), 
                               std::istream_iterator<std::string>());
#包括
#包括
#包括
std::istringstream(sen);
std::矢量字(std::istream_迭代器(流),
std::istreamu迭代器();
然后,您可以随意将其输出,或者直接在循环中执行,而无需使用向量:

for(std::vector<std::string>::const_iterator iter=
    words.begin(); iter!=words.end(); ++iter)
    std::cout << "found word: " << *iter << '\n';
for(std::vector::const_迭代器iter)=
words.begin();iter!=words.end();++iter)

STD:CUT< P>这不使用SUBR和查找,所以如果这是作业,你必须使用它,那么这不是一个好答案…但是我相信这是更好的方式来做你在C++中所要求的。它是未经测试但应该工作得很好。< /P>
//Create stringstream and insert your whole sentence into it.
std::stringstream ss;
ss << sen;

//Read out words one by one into a string - stringstream will tokenize them
//by the ASCII space character for you.
std::string myWord;
while (ss >> myWord)
    std::cout << myWord << std::endl;  //You can save it however you like here.
//创建stringstream并将整个句子插入其中。
std::stringstream-ss;
ss>myWord)

std::cout这不使用substr和find,所以如果这是作业,你必须使用它,那么这将不是一个好的答案。。。但我相信这是最好的方式来做你问的C++。它未经测试,但应该可以正常工作

//Create stringstream and insert your whole sentence into it.
std::stringstream ss;
ss << sen;

//Read out words one by one into a string - stringstream will tokenize them
//by the ASCII space character for you.
std::string myWord;
while (ss >> myWord)
    std::cout << myWord << std::endl;  //You can save it however you like here.
//创建stringstream并将整个句子插入其中。
std::stringstream-ss;
ss>myWord)

std::cout作为开始,不要使用“new”作为变量名。它已经在语言中被用于其他的东西。哦,所以它是一个保留字。我懂了。非常感谢。您可以使用
sen.find(“”)
来查找相同的空间,无论您调用它多少次。您可以使用
sen.substr(0,i)
将整个字符串返回给i,而不是只返回最新的单词。我认为您应该从开始,因为您似乎缺少一些非常基本的知识。相信我,如果你试图在没有任何好的书的情况下学习C++,你就在一个伤害的世界。作为一个开始的可能的复制品,不要把“新”作为变量名。它已经在语言中被用于其他的东西。哦,所以它是一个保留字。我懂了。非常感谢。您可以使用
sen.find(“”)
来查找相同的空间,无论您调用它多少次。您可以使用
sen.substr(0,i)
将整个字符串返回给i,而不是只返回最新的单词。我认为您应该从开始,因为您似乎缺少一些非常基本的知识。请相信我,如果你试图在没有任何好的书的情况下学习C++的话,你就会受到伤害。