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
使用string.size()时获取无限循环 #包括 #包括 #包括 #定义每行512的最大字符数 #按照第20行定义最大令牌数 #定义分隔符“” 使用名称空间std; int main() { string buf=“PicalCalculator(RandGen*RandGen,int-nPoints):RandGen(RandGen),nPoints(nPoints){”; 字符串buf1=buf; //将行解析为空白分隔标记 int n=0; 字符串令牌[每行最大令牌数]={}; 令牌[0]=strtok(&buf[0],分隔符); if(标记[0].size())//如果行为空,则为零 { 对于(n=1;n如果你只想从一个字符串中定义空白的“单词”(或令牌或你想调用它们),C++中有一些功能可以很简单地做到: #include <iostream> #include <fstream> #include <cstring> #define MAX_CHARS_PER_LINE 512 #define MAX_TOKENS_PER_LINE 20 #define DELIMITER " " using namespace std; int main () { string buf = "PiCalculator(RandGen *randGen, int nPoints) : randGen(randGen), nPoints(nPoints) {"; string buf1 = buf; // parse the line into blank-delimited tokens int n = 0; string token[MAX_TOKENS_PER_LINE] = {}; token[0] = strtok(&buf[0], DELIMITER); if (token[0].size()) // zero if line is blank { for (n = 1; n < MAX_TOKENS_PER_LINE; n++) { token[n] = strtok(0, DELIMITER); // subsequent tokens if (token[n].size() == 0) break; // no more tokens } } cout<<endl<<endl; // process (print) the tokens for (int i = 0; i < n; i++) { // n = #of tokens int pos=token[i].find('('); if(pos == token[i].size()) continue; else{ cout<<token[i].substr(0,pos)<<endl; } } return 0; } string buf=“PicalCalculator(RandGen*RandGen,int-nPoints):RandGen(RandGen),nPoints(nPoints){”; 标准:istringstream iss(buf); std::向量标记; std::copy(std::istream_迭代器(iss), std::istream_迭代器(), std::back_插入器(令牌));_C++_String_Infinite Loop - Fatal编程技术网

使用string.size()时获取无限循环 #包括 #包括 #包括 #定义每行512的最大字符数 #按照第20行定义最大令牌数 #定义分隔符“” 使用名称空间std; int main() { string buf=“PicalCalculator(RandGen*RandGen,int-nPoints):RandGen(RandGen),nPoints(nPoints){”; 字符串buf1=buf; //将行解析为空白分隔标记 int n=0; 字符串令牌[每行最大令牌数]={}; 令牌[0]=strtok(&buf[0],分隔符); if(标记[0].size())//如果行为空,则为零 { 对于(n=1;n如果你只想从一个字符串中定义空白的“单词”(或令牌或你想调用它们),C++中有一些功能可以很简单地做到: #include <iostream> #include <fstream> #include <cstring> #define MAX_CHARS_PER_LINE 512 #define MAX_TOKENS_PER_LINE 20 #define DELIMITER " " using namespace std; int main () { string buf = "PiCalculator(RandGen *randGen, int nPoints) : randGen(randGen), nPoints(nPoints) {"; string buf1 = buf; // parse the line into blank-delimited tokens int n = 0; string token[MAX_TOKENS_PER_LINE] = {}; token[0] = strtok(&buf[0], DELIMITER); if (token[0].size()) // zero if line is blank { for (n = 1; n < MAX_TOKENS_PER_LINE; n++) { token[n] = strtok(0, DELIMITER); // subsequent tokens if (token[n].size() == 0) break; // no more tokens } } cout<<endl<<endl; // process (print) the tokens for (int i = 0; i < n; i++) { // n = #of tokens int pos=token[i].find('('); if(pos == token[i].size()) continue; else{ cout<<token[i].substr(0,pos)<<endl; } } return 0; } string buf=“PicalCalculator(RandGen*RandGen,int-nPoints):RandGen(RandGen),nPoints(nPoints){”; 标准:istringstream iss(buf); std::向量标记; std::copy(std::istream_迭代器(iss), std::istream_迭代器(), std::back_插入器(令牌));

使用string.size()时获取无限循环 #包括 #包括 #包括 #定义每行512的最大字符数 #按照第20行定义最大令牌数 #定义分隔符“” 使用名称空间std; int main() { string buf=“PicalCalculator(RandGen*RandGen,int-nPoints):RandGen(RandGen),nPoints(nPoints){”; 字符串buf1=buf; //将行解析为空白分隔标记 int n=0; 字符串令牌[每行最大令牌数]={}; 令牌[0]=strtok(&buf[0],分隔符); if(标记[0].size())//如果行为空,则为零 { 对于(n=1;n如果你只想从一个字符串中定义空白的“单词”(或令牌或你想调用它们),C++中有一些功能可以很简单地做到: #include <iostream> #include <fstream> #include <cstring> #define MAX_CHARS_PER_LINE 512 #define MAX_TOKENS_PER_LINE 20 #define DELIMITER " " using namespace std; int main () { string buf = "PiCalculator(RandGen *randGen, int nPoints) : randGen(randGen), nPoints(nPoints) {"; string buf1 = buf; // parse the line into blank-delimited tokens int n = 0; string token[MAX_TOKENS_PER_LINE] = {}; token[0] = strtok(&buf[0], DELIMITER); if (token[0].size()) // zero if line is blank { for (n = 1; n < MAX_TOKENS_PER_LINE; n++) { token[n] = strtok(0, DELIMITER); // subsequent tokens if (token[n].size() == 0) break; // no more tokens } } cout<<endl<<endl; // process (print) the tokens for (int i = 0; i < n; i++) { // n = #of tokens int pos=token[i].find('('); if(pos == token[i].size()) continue; else{ cout<<token[i].substr(0,pos)<<endl; } } return 0; } string buf=“PicalCalculator(RandGen*RandGen,int-nPoints):RandGen(RandGen),nPoints(nPoints){”; 标准:istringstream iss(buf); std::向量标记; std::copy(std::istream_迭代器(iss), std::istream_迭代器(), std::back_插入器(令牌));,c++,string,infinite-loop,C++,String,Infinite Loop,上述代码将把字符串buf中的所有(以空格分隔的)“标记”复制到向量标记中 参考资料: 它对我来说崩溃了…而且不会陷入无休止的循环。token[n]=strtok(0,分隔符);//后续的token->这是一种很好的崩溃方式。我想你指的是token[n]。c_str()在那里,对吗?为什么你在C++程序中使用Strutk?我建议阅读来更好地了解如何调试这个。你知道返回代码当没有更多的令牌被发现时。 Strtok < /C>是一个邪恶的函数。它修改源并返回NULL,这可能导致STD::st

上述代码将把字符串
buf
中的所有(以空格分隔的)“标记”复制到向量
标记中

参考资料:


它对我来说崩溃了…而且不会陷入无休止的循环。
token[n]=strtok(0,分隔符);//后续的token
->这是一种很好的崩溃方式。我想你指的是token[n]。c_str()在那里,对吗?为什么你在C++程序中使用Strutk?我建议阅读来更好地了解如何调试这个。你知道返回代码<空NU/C>当没有更多的令牌被发现时。<代码> Strtok < /C>是一个邪恶的函数。它修改源并返回NULL,这可能导致STD::string构造器崩溃。我不推荐用C++代码来使用它,它太粗糙了。@ JoachimPileborg:是的,我知道。
string buf = "PiCalculator(RandGen *randGen, int nPoints) : randGen(randGen), nPoints(nPoints) {";

std::istringstream iss(buf);
std::vector<std::string> tokens;

std::copy(std::istream_iterator<std::string>(iss),
          std::istream_iterator<std::string>(),
          std::back_inserter(tokens));