Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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++。本书介绍了以下函数,可以根据空格字符将字符串拆分为子字符串 vector<string> split(const string& s) { vector<string> ret; typedef string::size_type string_size; string_size i = 0; while (i != s.size()) { // ignore leading blanks // invariant: characters in range[original i, current i) are all spaces while (i != s.size() && isspace(s[i])) ++i; // find the end of next word string_size j = i; // invariant: none of the characters in range [original j, current j) is a space while (j != s.size() && !isspace(s[j])) j++; if (i != j) { // copy from s starting at i and taking j-i chars ret.push_back(s.substr(i, j - i)); i = j; } } return ret; }_C++ - Fatal编程技术网

C++;用于拆分字符串的函数 我最近开始使用C++加速书学习C++。本书介绍了以下函数,可以根据空格字符将字符串拆分为子字符串 vector<string> split(const string& s) { vector<string> ret; typedef string::size_type string_size; string_size i = 0; while (i != s.size()) { // ignore leading blanks // invariant: characters in range[original i, current i) are all spaces while (i != s.size() && isspace(s[i])) ++i; // find the end of next word string_size j = i; // invariant: none of the characters in range [original j, current j) is a space while (j != s.size() && !isspace(s[j])) j++; if (i != j) { // copy from s starting at i and taking j-i chars ret.push_back(s.substr(i, j - i)); i = j; } } return ret; }

C++;用于拆分字符串的函数 我最近开始使用C++加速书学习C++。本书介绍了以下函数,可以根据空格字符将字符串拆分为子字符串 vector<string> split(const string& s) { vector<string> ret; typedef string::size_type string_size; string_size i = 0; while (i != s.size()) { // ignore leading blanks // invariant: characters in range[original i, current i) are all spaces while (i != s.size() && isspace(s[i])) ++i; // find the end of next word string_size j = i; // invariant: none of the characters in range [original j, current j) is a space while (j != s.size() && !isspace(s[j])) j++; if (i != j) { // copy from s starting at i and taking j-i chars ret.push_back(s.substr(i, j - i)); i = j; } } return ret; },c++,C++,当我在终端中编译函数时,如下所示 g++ -o test_split_function test_split_function.cpp 我没有错误。但是,当我运行脚本时,它会继续运行,并且不会在命令行中生成任何输出。我不明白为什么,因为基于空格将字符串拆分为单词的函数确实在同一个字符串上运行 问题:我做错了什么?是s[j]!=','语句不正确?有两部分需要替换isspace()函数,一部分用于i循环: while(i!=s.size()&&s[i]==',') ++一,; 一个用于j循环:

当我在终端中编译函数时,如下所示

g++ -o test_split_function test_split_function.cpp
我没有错误。但是,当我运行脚本时,它会继续运行,并且不会在命令行中生成任何输出。我不明白为什么,因为基于空格将字符串拆分为单词的函数确实在同一个字符串上运行


问题:我做错了什么?是
s[j]!=','语句不正确?

有两部分需要替换
isspace()
函数,一部分用于
i
循环:

while(i!=s.size()&&s[i]==',')
++一,;
一个用于
j
循环:

while(j!=s.size()&&s[j]!=',')
j++;

这将起作用。

我对这段代码做了一些更改。将ispace函数替换为
s[j]=','
s[j]!=','
查看它

 #include<bits/stdc++.h>
 using namespace std;

 vector<string> split(const string& s)
 {
 vector<string> ret;
 typedef string::size_type string_size;
 string_size i = 0;

 while (i != s.size()) {
    // ignore leading blanks
    // invariant: characters in range[original i, current i) are all spaces
    while (i != s.size() && s[i]==',')
        ++i;

    // find the end of next word
    string_size j = i;
    // here i changed isspace with s[j]!=','
    while (j != s.size() && s[j]!=',')
        j++;

    if (i != j) {
        // copy from s starting at i and taking j-i chars
        ret.push_back(s.substr(i, j - i));
        i = j;
     }
   }
   return ret;
 }

int main()
{

 string test_string = "this is ,a test ";

 vector<string> test = split(test_string);

 for (vector<string>::const_iterator it = test.begin(); it != test.end(); ++it)
    cout << *it << endl;

 return 0;
}
#包括
使用名称空间std;
向量拆分(常量字符串和s)
{
向量ret;
typedef string::size\u type string\u size;
字符串大小i=0;
而(i!=s.size()){
//忽略前导空格
//不变量:范围[原始i,当前i]中的字符都是空格
而(i!=s.size()&&s[i]==',')
++一,;
//找到下一个单词的结尾
串_尺寸j=i;
//在这里,我用s[j]!=','更改了isspace
而(j!=s.size()&&s[j]!=',')
j++;
如果(i!=j){
//从s处复制,从i开始,取j-i字符
后推(s.substr(i,j-i));
i=j;
}
}
返回ret;
}
int main()
{
string test_string=“这是一个测试”;
向量测试=拆分(测试字符串);
for(vector::const_迭代器it=test.begin();it!=test.end();++it)

您是否还需要更改对
isspace
的另一个调用。使用IDE和调试器而不是编译到终端,您可以停止/检查代码行为错误的原因。我没有收到错误。--无错误编译仅意味着程序不包含语法错误。这与程序逻辑正确与否。而且,
 #include<bits/stdc++.h>
 using namespace std;

 vector<string> split(const string& s)
 {
 vector<string> ret;
 typedef string::size_type string_size;
 string_size i = 0;

 while (i != s.size()) {
    // ignore leading blanks
    // invariant: characters in range[original i, current i) are all spaces
    while (i != s.size() && s[i]==',')
        ++i;

    // find the end of next word
    string_size j = i;
    // here i changed isspace with s[j]!=','
    while (j != s.size() && s[j]!=',')
        j++;

    if (i != j) {
        // copy from s starting at i and taking j-i chars
        ret.push_back(s.substr(i, j - i));
        i = j;
     }
   }
   return ret;
 }

int main()
{

 string test_string = "this is ,a test ";

 vector<string> test = split(test_string);

 for (vector<string>::const_iterator it = test.begin(); it != test.end(); ++it)
    cout << *it << endl;

 return 0;
}