重新运行程序和字符串流?

rerun a program and stringstream?

本文关键字:字符串 程序 重新运行      更新时间:2023-10-16

我有这个程序:

#include <iostream>
#include <vector>
#include <math.h>
#include <cstdlib>
#include <iomanip>  
#include <sstream>
using namespace std;


void ProvaErrore() {
string line, line1;
char delim = ' ';
ifstream myfile("DatiMemoria.txt");
int i = 0, j = 0;
vector<vector<double>> vettore(40);
vector<double> risultato;
while (getline(myfile, line1))
{
stringstream ss(line1);
while (getline(ss, line, delim))
{
if (j == 12)
{
i++;
j = 0;
}
if (j == 0)
{
if (line == 'p')
{
risultato.push_back(0);
}
else
{
risultato.push_back(1);
}
}
if (j == 1)
{
if (line == 'x')
{
vettore[i].push_back(0);
}
else
{
vettore[i].push_back(1);
}
}
if (j == 2)
{
if (line == 's')
{
vettore[i].push_back(0);
vettore[i].push_back(0);
}
if (line == 'y')
{
vettore[i].push_back(0);
vettore[i].push_back(1);
}
if (line == 'f')
{
vettore[i].push_back(1);
vettore[i].push_back(0);
}
}
if (j == 3)
{
if (line == 'w')
{
vettore[i].push_back(0);
}
if (line == 'n')
{
vettore[i].push_back(1);
}
}
if (j == 4)
{
if (line == 't')
{
vettore[i].push_back(0);
}
if (line == 'f')
{
vettore[i].push_back(1);
}
}
if (j == 5)
{
if (line == 'l')
{
vettore[i].push_back(0);
vettore[i].push_back(0);
}
if (line == 'p')
{
vettore[i].push_back(0);
vettore[i].push_back(1);
}
if (line == 'n')
{
vettore[i].push_back(1);
vettore[i].push_back(0);
}
if (line == 'a')
{
vettore[i].push_back(1);
vettore[i].push_back(1);
}
}
if (j == 6)
{
if (line == 'b')
{
vettore[i].push_back(0);
}
if (line == 'n')
{
vettore[i].push_back(1);
}
}
if (j == 7)
{
if (line == 'k')
{
vettore[i].push_back(0);
vettore[i].push_back(0);
}
if (line == 'w')
{
vettore[i].push_back(0);
vettore[i].push_back(1);
}
if (line == 'n')
{
vettore[i].push_back(1);
vettore[i].push_back(0);
}
if (line == 'p')
{
vettore[i].push_back(1);
vettore[i].push_back(1);
}
}
if (j == 8)
{
if (line == 'c')
{
vettore[i].push_back(0);
}
if (line == 'e')
{
vettore[i].push_back(1);
}
}
if (j == 9)
{
if (line == 'k')
{
vettore[i].push_back(0);
}
if (line == 'n')
{
vettore[i].push_back(1);
}
}
if (j == 10)
{
if (line == 's')
{
vettore[i].push_back(0);
}
if (line == 'v')
{
vettore[i].push_back(1);
}
}
if (j == 11)
{
if (line == 'g')
{
vettore[i].push_back(0);
}
if (line == 'u')
{
vettore[i].push_back(1);
}
}
j++;
}
}
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 14; j++)
{
cout << vettore[i][j]<<" ";
}
cout << "n";
}
}

使用该 txt 文件:

e x y w t a b n c n s g
p x s w t p n k e k s g
p x y n t p n k e n v u
p x y w t p n w e k s g
p x s w t p n n e n s g
p x y w t p n w e n s g
p x y w t p n k e n s g
p x y w t p n k e k s g
p x y n t p n p e k v u
e f f n f n n p e n v u
p f y n t p n k e k v u
p x s w t p n w e n s g
p f y n t p n k c n v u
p x y n t p n n e n v u
p x s w t p n k e n s g
p f s n t p n p e k v u
p x y n t p n p e n v u
p x y w t p n n e k s g
p x y n t p n k e k v u
e x f n f n n n e k v u
e x s w t l b k c k s g
e x f n f n n k e n v u
e x y w t l b w c k s g
p f y n t p n p e n v u
e x s w t l b n c n s g
e x y w t a b w c n s g
e x y w t a b k c n s g
e x y w t l b k c k s g
e x f n f n n p e k v u
p x y n t p n n e k v u
e f f n f n n k e k v u
e x s w t l b w c n s g
e f f n f n n k e n v u
e x f n f n n n e n v u
e x s w t a b k c n s g
e f f n f n n p e k v u
e x f n f n n p e n v u
e x y w t l b n c k s g
e x f n f n n k e k v u
p x y w t p n n e n s g

这是一个对数据分析有用的简单程序。我有一个 40 行的文件。每行都是蘑菇,每列都是一个特征,例如第一列是可食用的或有毒的。我想读取该文件并将字母转换为数字(以字节数字为单位(,以便将它们放入神经网络中。通常我用python这样做,但我想尝试用c ++进行尝试

当我第一次运行它时,一切都很好。但是如果我尝试重新运行它,则会出现此错误:

error: no matching constructor for initialization of 'std::stringstream' (aka 'basic_stringstream<char, char_traits<char>,
allocator<char> >')
stringstream ss(line1);
^  ~~~~~
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.20.27508includesstream:531:14: note: candidate constructor not viable: no known conversion from 'std::string' (aka 'basic_string<char, char_traits<char>,
allocator<char> >') to 'ios_base::openmode' (aka 'int') for 1st argument
explicit basic_stringstream(ios_base::openmode _Mode = ios_base::in | ios_base::out)
^
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.20.27508includesstream:539:5: note: candidate constructor not viable: no known conversion from 'std::string' (aka 'basic_string<char, char_traits<char>,
allocator<char> >') to 'std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' for 1st
argument
basic_stringstream(basic_stringstream&& _Right) : _Mybase(&_Stringbuffer) { // construct by moving _Right
^
C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.20.27508includesstream:562:5: note: candidate constructor not viable: no known conversion from 'std::string' (aka 'basic_string<char, char_traits<char>,
allocator<char> >') to 'const std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >' for
1st argument
basic_stringstream(const basic_stringstream&) = delete;

为什么?

看起来您正在尝试执行文件 IO,首先,将文件拆分为行,其次,按空格拆分每行(在您的示例中,将它们拆分为 1 个字符长的字符串(。相反,您可以对所有其他字符进行基于范围的循环(特别是因为每行都有相同数量的字符(。像下面这样打开文件将完全避免使用std::stringstream

std::string current_line;
/* vectors here */
std::ifstream myfile("DatiMemoria.txt");
// You had int j(char_num) = 0, i(line_num) = 0;
// I renamed them to make it clear what they're for
// Also, `char_num` is not initialized until later
unsigned line_num = 0;
if (myfile.is_open())
{
while (std::getline(myfile, current_line))
{
for (int char_num = 0; char_num <= 2 * (12 - 1); char_num += 2)
{
// use:
//     current_line.at(char_num)
// to get the current character. Then do what you need
// to with that character. Then, the loop will move to 
// the next character.
}
line_num++;
}
}

但是,只是为了让我忽略您的问题,您的编译器似乎想要对stringstream使用与接受const std::string&的构造函数不同的构造函数。事实上,如果你看一下你的错误日志,它只显示 3 个可能的构造函数。这就是为什么我倾向于尽量避免使用stringstream

但是,如果您死心塌地地使用stringstream,您可以创建流,然后通过两个单独的步骤将字符串推送到其中。

std::string str;
std::stringstream sstrm;
sstrm << str;

或者,如果字符串给你带来错误,

std::string str;
std::stringstream sstrm;
sstrm << str.c_str();

我无法想象任何理智的编译器对此有问题。

你忘了

#include <fstream>

std::ifstream的标题


if (line == 'p')

不起作用,因为linestd::string.在这里你想要

if (line[0] == 'p')

以接收目标字符。因此,将所有line ==实例替换为line[0] ==


作为建议,请尝试提高代码的质量。您可以通过将结构分成多个部分并使用函数来处理每个任务来实现这一点。

另外,你不应该use namespace std;


完整代码:

#include <iostream>
#include <vector>
#include <math.h>
#include <cstdlib>
#include <iomanip>  
#include <sstream>
#include <fstream>
using namespace std;

void ProvaErrore() {
string line, line1;
char delim = ' ';
ifstream myfile("DatiMemoria.txt");
int i = 0, j = 0;
vector<vector<double>> vettore(40);
vector<double> risultato;
while (getline(myfile, line1))
{
stringstream ss(line1);
while (getline(ss, line, delim))
{
if (j == 12)
{
i++;
j = 0;
}
if (j == 0)
{
if (line[0] == 'p')
{
risultato.push_back(0);
}
else
{
risultato.push_back(1);
}
}
if (j == 1)
{
if (line[0] == 'x')
{
vettore[i].push_back(0);
}
else
{
vettore[i].push_back(1);
}
}
if (j == 2)
{
if (line[0] == 's')
{
vettore[i].push_back(0);
vettore[i].push_back(0);
}
if (line[0] == 'y')
{
vettore[i].push_back(0);
vettore[i].push_back(1);
}
if (line[0] == 'f')
{
vettore[i].push_back(1);
vettore[i].push_back(0);
}
}
if (j == 3)
{
if (line[0] == 'w')
{
vettore[i].push_back(0);
}
if (line[0] == 'n')
{
vettore[i].push_back(1);
}
}
if (j == 4)
{
if (line[0] == 't')
{
vettore[i].push_back(0);
}
if (line[0] == 'f')
{
vettore[i].push_back(1);
}
}
if (j == 5)
{
if (line[0] == 'l')
{
vettore[i].push_back(0);
vettore[i].push_back(0);
}
if (line[0] == 'p')
{
vettore[i].push_back(0);
vettore[i].push_back(1);
}
if (line[0] == 'n')
{
vettore[i].push_back(1);
vettore[i].push_back(0);
}
if (line[0] == 'a')
{
vettore[i].push_back(1);
vettore[i].push_back(1);
}
}
if (j == 6)
{
if (line[0] == 'b')
{
vettore[i].push_back(0);
}
if (line[0] == 'n')
{
vettore[i].push_back(1);
}
}
if (j == 7)
{
if (line[0] == 'k')
{
vettore[i].push_back(0);
vettore[i].push_back(0);
}
if (line[0] == 'w')
{
vettore[i].push_back(0);
vettore[i].push_back(1);
}
if (line[0] == 'n')
{
vettore[i].push_back(1);
vettore[i].push_back(0);
}
if (line[0] == 'p')
{
vettore[i].push_back(1);
vettore[i].push_back(1);
}
}
if (j == 8)
{
if (line[0] == 'c')
{
vettore[i].push_back(0);
}
if (line[0] == 'e')
{
vettore[i].push_back(1);
}
}
if (j == 9)
{
if (line[0] == 'k')
{
vettore[i].push_back(0);
}
if (line[0] == 'n')
{
vettore[i].push_back(1);
}
}
if (j == 10)
{
if (line[0] == 's')
{
vettore[i].push_back(0);
}
if (line[0] == 'v')
{
vettore[i].push_back(1);
}
}
if (j == 11)
{
if (line[0] == 'g')
{
vettore[i].push_back(0);
}
if (line[0] == 'u')
{
vettore[i].push_back(1);
}
}
j++;
}
}
for (int i = 0; i < 40; i++)
{
for (int j = 0; j < 14; j++)
{
cout << vettore[i][j] << " ";
}
cout << "n";
}
}

输出:

0 0 1 0 0 1 1 0 1 0 0 1 0 0
0 0 0 0 0 0 1 1 0 0 1 0 0 0
0 0 1 1 0 0 1 1 0 0 1 1 1 1
0 0 1 0 0 0 1 1 0 1 1 0 0 0
0 0 0 0 0 0 1 1 1 0 1 1 0 0
0 0 1 0 0 0 1 1 0 1 1 1 0 0
0 0 1 0 0 0 1 1 0 0 1 1 0 0
0 0 1 0 0 0 1 1 0 0 1 0 0 0
0 0 1 1 0 0 1 1 1 1 1 0 1 1
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