Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.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++_String_Parsing_Io_Formatting - Fatal编程技术网

C++ 解析格式化输入时分词的问题

C++ 解析格式化输入时分词的问题,c++,string,parsing,io,formatting,C++,String,Parsing,Io,Formatting,我得到的输出: Math 3 2 Math 4 3 Math 5 4 Phys 3 1 Math 3 1 Comp 3 2 编辑:: 为了澄清,向量元素的构造是正确的。如果我从for(auto a:vec)中打印一个,它会像文件一样逐行给我提供信息。当我试图从向量中的每个字符构建单词时。我想问的是,我可以做些什么来指定有一个新的行,以便 Math 3 2Math 4 3Math 5 4Phys 3 1Math 3 1Comp 3 3 当它到达一行的末尾时,不会一直添加到该行。不要这样做 li

我得到的输出:

Math 3 2
Math 4 3
Math 5 4
Phys 3 1
Math 3 1
Comp 3 2
编辑::

为了澄清,向量元素的构造是正确的。如果我从for(auto a:vec)中打印一个,它会像文件一样逐行给我提供信息。当我试图从向量中的每个字符构建单词时。我想问的是,我可以做些什么来指定有一个新的行,以便

Math
3
2Math
4
3Math
5
4Phys
3
1Math
3
1Comp
3
3
当它到达一行的末尾时,不会一直添加到该行。

不要这样做

line += a[i] 
它不会按你期望的方式工作

取而代之的是

while (!in.eof()) { ... }
这样做的原因是,只有在文件结束时真正尝试读取时,才会设置
eof
标志。这意味着您将循环一次到多次,尝试读取一条不存在的行,然后将其添加到向量中


还有另一种在空间边界上分离“单词”的方法,使用
std::istringstream
和普通输入操作符
>

while (std::getline(...)) { ... }
不要这样做

line += a[i] 
它不会按你期望的方式工作

取而代之的是

while (!in.eof()) { ... }
这样做的原因是,只有在文件结束时真正尝试读取时,才会设置
eof
标志。这意味着您将循环一次到多次,尝试读取一条不存在的行,然后将其添加到向量中


还有另一种在空间边界上分离“单词”的方法,使用
std::istringstream
和普通输入操作符
>

while (std::getline(...)) { ... }

你也可以用这样的向量逐行读取

矢量定义

std::istringstream is{a};

std::string word;
while (is >> word)
{
    // Do something with `word`
}
struct VectorName{
std::字符串str;
浮点数m1;
浮点数m2;
};
std::vector smthVector_u2;;
函数的使用

    struct VectorName {
       std::string str;
       float num1;
       float num2;
};

std::vector <VectorName> smthVector_;
VectorName向量;
std::字符串str;
char buf_1[50];
while(std::getline(in,str))
{
如果(sscanf(str.c_str(),%s%f%f),buf_1,&vector.num1,&vector.num2)==3)
{
vector.str=buf_1;
smthVector\u推回(vector);
}
其他的

std::cout您也可以使用如下向量逐行读取

矢量定义

std::istringstream is{a};

std::string word;
while (is >> word)
{
    // Do something with `word`
}
struct VectorName{
std::字符串str;
浮点数m1;
浮点数m2;
};
std::vector smthVector_u2;;
函数的使用

    struct VectorName {
       std::string str;
       float num1;
       float num2;
};

std::vector <VectorName> smthVector_;
VectorName向量;
std::字符串str;
char buf_1[50];
while(std::getline(in,str))
{
如果(sscanf(str.c_str(),%s%f%f),buf_1,&vector.num1,&vector.num2)==3)
{
vector.str=buf_1;
smthVector\u推回(vector);
}
其他的

std::cout这里的实际问题是代码逻辑错误。下面是打印输出时所做操作的伪代码:

   VectorName vector;
   std::string str;
   char buf_1[50];
   while(std::getline(in, str))
   {
       if(sscanf(str.c_str(), "%s %f %f", buf_1, &vector.num1, &vector.num2) == 3)
       {
           vector.str = buf_1;
           smthVector_push_back(vector);
       }
        else
            std::cout << "No param in string  " << str << std::endl;
   }
现在看一看这行
Math 4 3
~>打印单词“4”后,此代码将字符
'3'
添加到行中,但不会将其打印为单词,因此
3
是下一行单词的开头

还要注意的是,您的代码比需要的复杂得多。它可能看起来像这样:

for every line:
    for every character in the line:
        if it is alphanumerical character
            then add it to the word
        else
            print the so-far built word
std::字符串字;
对于(大小i=0;i>word)

std::cout这里的实际问题是代码逻辑错误。下面是打印输出时所做操作的伪代码:

   VectorName vector;
   std::string str;
   char buf_1[50];
   while(std::getline(in, str))
   {
       if(sscanf(str.c_str(), "%s %f %f", buf_1, &vector.num1, &vector.num2) == 3)
       {
           vector.str = buf_1;
           smthVector_push_back(vector);
       }
        else
            std::cout << "No param in string  " << str << std::endl;
   }
现在看一看这行
Math 4 3
~>打印单词“4”后,此代码将字符
'3'
添加到行中,但不会将其打印为单词,因此
3
是下一行单词的开头

还要注意的是,您的代码比需要的复杂得多。它可能看起来像这样:

for every line:
    for every character in the line:
        if it is alphanumerical character
            then add it to the word
        else
            print the so-far built word
std::字符串字;
对于(大小i=0;i>word)

std::cout和in
虽然(std::getline(in,line)){
循环,但也可以使用
line.empty()
为了确保没有读取空行:)让我澄清一下。构建向量后的输出为我提供了正确的向量元素。这是我遇到问题的第二部分。当我从chars构建单词时。值得注意的是
std::istringstream是{a}
是一种C++11语法。@同志,我刚刚用另一种在空白处拆分一行的方法更新了我的答案。@JoachimPileborg。我很感激你的答案。我尝试了这个方法,它很有效,但这是另一种答案。有没有一种简单的方法可以使用内置函数(如ispunt())来识别是否有新行?同时在
中(std::getline(in,line)){
loop,也可以使用
line.empty()
来确保没有读取空行:)让我澄清一下。构建向量后的输出为我提供了正确的向量元素。这是我遇到问题的第二部分。当我从chars构建单词时。值得注意的是,
std::istringstream是{a}
是一种C++11语法。@同志,我刚刚用另一种在空白处拆分一行的方法更新了我的答案。@JoachimPileborg。我很感激你的答案。我尝试过这个方法,但它是一种替代答案。有没有一种简单的方法来使用内置函数,如ispunt()要确定有一个新行吗?只是一个旁注:我个人更喜欢使用传统的
for
循环,而不是
for(auto a:vec)
,以避免出现诸如
if(!(isspace(*I)| ispunt(*I))line+=*I;
:)之类的代码,我打算用另一种方式:
for(auto c:a)
这里非常适合。为了消除另一个冗余,您可以直接吐出
,无需构建字符串流。同志:
std::getline
不提供行终止符。提示#2:在什么情况下可以从
a
读取字符而不吐新行?提示#3:尝试添加在每个输入行的末尾加一个空格,看看这里发生了什么..for(auto c:a)打印每个字符。