C++ 如何在c/c++;?

C++ 如何在c/c++;?,c++,c,parsing,scanf,C++,C,Parsing,Scanf,我有一个包含以下行的文件: 25 1 0 0 0 0 27 1 0 0 0 0 20 0 0 0 0 0 32 1 0 0 0 0 23 1 0 0 0 0 16 0 0 0 0 0 28 1 0 0 0 0 首先,我将value=1的每一行作为字符串数组的元素存储在第二列中。我通过for循环调用存储的元素,并将元素的内容解析为整数。如何在C++中实现这一点? #include <vector> #include <iostream> #include <str

我有一个包含以下行的文件:

25 1 0 0 0 0
27 1 0 0 0 0
20 0 0 0 0 0
32 1 0 0 0 0
23 1 0 0 0 0
16 0 0 0 0 0
28 1 0 0 0 0
首先,我将value=1的每一行作为字符串数组的元素存储在第二列中。我通过for循环调用存储的元素,并将元素的内容解析为整数。如何在C++中实现这一点?

#include <vector> 
#include <iostream>
#include <string>
using namespace std;
   .....
   .....
   .....
   .....
char line[100];
vector<string> vec_line;
string Vline;
int trimVal, compa, nil0, nil1, nil2, nil3;
  ......
  ......
  ......
//then store lines as strings in vec_line as required by the premise above
// after that, retrieve the contents of the elements of the vector
for (int iline=0; iline<vec_line.size(); iline++) {
   cout<<"iline  "<<iline<<"  vec_line[iline]";
   //then I want to parse contents of each string element and store them integer formats
   sscanf(vec_line[iline], "%d %d %d %d %d %d", &trimVal, &compa, &nil0, &nil1, &nil2, &nil3); //how would one do this simple parsing task in c and c++?
}
#包括
#包括
#包括
使用名称空间std;
.....
.....
.....
.....
字符行[100];
矢量矢量线;
弦线;
int trimVal、compa、nil0、nil1、nil2、nil3;
......
......
......
//然后根据上述前提的要求,将行作为字符串存储在vec_行中
//然后,检索向量元素的内容
对于(int-iline=0;iline这是不正确的:

sscanf(vec_line[iline], "%d %d ...
作为的第一个参数是
const char*
,而不是
std::string
。更改为:

sscanf(vec_line[iline].c_str(), "%d %d ...
建议检查
sscanf()
的返回值,以确保完成所有预期的分配:

if (6 == sscanf(vec_line[iline].c_str(), "%d %d ...
这是不正确的:

sscanf(vec_line[iline], "%d %d ...
作为的第一个参数是
const char*
,而不是
std::string
。更改为:

sscanf(vec_line[iline].c_str(), "%d %d ...
建议检查
sscanf()
的返回值,以确保完成所有预期的分配:

if (6 == sscanf(vec_line[iline].c_str(), "%d %d ...

<>代码> sScNF< /Cord>是C函数,既然你已经使用C++流来输出(并且可能从文件中读取),那么最好使用<代码> StrugSuths//Cuff>在字符串和数据类型之间转换。 例如:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
   stringstream sstr;
   string mystr = "123";
   int i;

   sstr << mystr;

   sstr >> i;

   cout << i << endl;

   return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
stringstream sstr;
字符串mystr=“123”;
int i;
sstr>i;
如果愿意,可以将
运算符直接输入到
int
double


希望这有帮助。

<代码> sSCANF是C函数,既然你已经使用C++流来输出(并且可能从文件中读取),你最好使用<代码> StrugSuths//Cuff>在字符串和数据类型之间转换。 例如:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
   stringstream sstr;
   string mystr = "123";
   int i;

   sstr << mystr;

   sstr >> i;

   cout << i << endl;

   return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
stringstream sstr;
字符串mystr=“123”;
int i;
sstr>i;
如果愿意,可以将
运算符直接输入到
int
double

希望这有帮助。

除非我需要将每一行存储为字符串,否则我会执行以下操作:

struct row { 
    static const int columns = 6;
    std::vector<int> column_data;

    // should we keep or ignore a row? Ignore if column 1 != 1.
    struct filter {
        bool operator()(row const &r) { return r.column_data[1] != 1; }
    };

    // read in a row.
    friend std::istream &operator>>(std::istream &is, row &r) {
        r.column_data.resize(row::columns);
        for (int i=0; i<row::columns; i++)
            is >> r.columns[i];
        return is;
    }

    // write a row out to a file.
    friend std::ostream &operator<<(std::ostream &os, row const &r) { 
        std::copy(r.column_data.begin(), r.column_data.end(),
                  std::ostream_iterator<int>(os, " "));
        return os;
};
std::vector<row> data;

std::remove_copy_if(std::istream_iterator<row>(input_file),
                    std::istream_iterator<row>(),
                    std::back_inserter(data),
                    row::filter());

std::copy(data.begin(), data.end(), 
          std::ostream_iterator<row>(output_file, "\n"));
struct行{
静态常量int列=6;
std::向量列数据;
//我们应该保留还是忽略一行?如果列1!=1,则忽略。
结构过滤器{
bool运算符()(行常量&r){返回r.column_数据[1]!=1;}
};
//连续阅读。
friend std::istream&operator>>(std::istream&is,row&r){
r、 column_data.resize(行::列);
对于(int i=0;i>r.columns[i];
回报是;
}
//将一行写入文件。
friend std::ostream&operator除非我需要将每一行存储为字符串,否则我会执行以下操作:

struct row { 
    static const int columns = 6;
    std::vector<int> column_data;

    // should we keep or ignore a row? Ignore if column 1 != 1.
    struct filter {
        bool operator()(row const &r) { return r.column_data[1] != 1; }
    };

    // read in a row.
    friend std::istream &operator>>(std::istream &is, row &r) {
        r.column_data.resize(row::columns);
        for (int i=0; i<row::columns; i++)
            is >> r.columns[i];
        return is;
    }

    // write a row out to a file.
    friend std::ostream &operator<<(std::ostream &os, row const &r) { 
        std::copy(r.column_data.begin(), r.column_data.end(),
                  std::ostream_iterator<int>(os, " "));
        return os;
};
std::vector<row> data;

std::remove_copy_if(std::istream_iterator<row>(input_file),
                    std::istream_iterator<row>(),
                    std::back_inserter(data),
                    row::filter());

std::copy(data.begin(), data.end(), 
          std::ostream_iterator<row>(output_file, "\n"));
struct行{
静态常量int列=6;
std::向量列数据;
//我们应该保留还是忽略一行?如果列1!=1,则忽略。
结构过滤器{
bool运算符()(行常量&r){返回r.column_数据[1]!=1;}
};
//连续阅读。
friend std::istream&operator>>(std::istream&is,row&r){
r、 column_data.resize(行::列);
对于(int i=0;i>r.columns[i];
回报是;
}
//将一行写入文件。

friend std::ostream&operator总是有lex和yacc(or and)

总是有lex和yacc(or and)