问题描述
您好!
以下是我的代码,它应该执行以下操作:
1)将一行的每个标记存储为向量的元素,并将此
矢量存储在另一个矢量中。
2)如果数据是> 0,它应该存储为1,否则,它应该存储为
为零,在另一个名为binaryVec的向量中。
它正确编译,但是,对于向量中的每个元素
,输出始终为1,即使它是< 0.有人可以告诉我我在做什么吗?b $ b做错了吗?
#include< fstream>
# include< string>
#include< iomanip>
#include< iostream>
#include< istream>
#include< cstdlib>
#include< vector>
#include< sstream>
using namespace std ;
typedef vector< string>行;
int main()
{
ifstream inFile;
inFile.open( " data.txt");
if(inFile.fail())
{
cout<< \ n文件未成功打开 <<结束;
退出(1);
}
vector< lines> SourceVector;
string one_line;
char token [128];
istringstream lineStream(one_line);
vector< bool> binaryVec;
bool binary;
while(getline(inFile,one_line,''\ n''))
{
cout<< 从文件中读取: << one_line<< endl;
vector< string> text;
istringstream lineStream(one_line);
while(lineStream>> token)
{
cout<< "令牌: <<令牌<< endl;
text.push_back(token); //在文本向量中存储标记。
double newToken = atof(标记);
if(newToken> = 0)
{二进制=真; }
else
{binary = false; }
binaryVec.push_back(二进制);
}
SourceVector.push_back(text); //将文本向量存储在
SourceVector。
//测试binaryVec的输出
for(int i = 1; i < binaryVec.size(); i ++)
{
cout<< 二元元素 << i<< "是 << binaryVec [i]<<
endl;
cout<< text.front()<<结束;
}
}
系统(PAUSE);
返回0;
}
顺便说一下,文件看起来像:
A 0.1 0.2 0.3 0.4
B 0.2 0 0.4 0
..
提前付款!
Hello!
Below is my code which is supposed to do the following:
1) store each token of a line as elements of a vector, and store this
vector in another vector.
2) if the data is > 0, it should be stored as 1, else, it should be stored
as zero, in another vector called binaryVec.
It compiles correctly, however, the output is always 1 for every element
in the vector, even if it is < 0. Can someone please tell me what i''m
doing wrong?
#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
#include <istream>
#include <cstdlib>
#include <vector>
#include <sstream>
using namespace std;
typedef vector<string> lines;
int main()
{
ifstream inFile;
inFile.open("data.txt");
if (inFile.fail())
{
cout <<"\nThe file was not successfully opened" << endl;
exit(1);
}
vector<lines> SourceVector;
string one_line;
char token[128];
istringstream lineStream(one_line);
vector<bool> binaryVec;
bool binary;
while (getline(inFile, one_line, ''\n''))
{
cout << "Read from file: " << one_line << endl;
vector<string> text;
istringstream lineStream( one_line );
while( lineStream >> token )
{
cout << " Token: " << token << endl;
text.push_back( token ); // stores tokens in text vector.
double newToken = atof(token);
if ( newToken >= 0)
{ binary = true; }
else
{ binary = false; }
binaryVec.push_back(binary);
}
SourceVector.push_back( text ); // stores text vector in
SourceVector.
//Testing output for binaryVec
for( int i = 1; i < binaryVec.size(); i++ )
{
cout << "Binary element " << i << " is " << binaryVec[i] <<
endl;
cout << text.front() << endl;
}
}
system("PAUSE");
return 0;
}
by the way, the file looks something like:
A 0.1 0.2 0.3 0.4
B 0.2 0 0.4 0
..
Thanx in advance!
推荐答案
我没有看到任何负数在你的数据文件中...
-shez-
I don''t see any negative numbers in your data file...
-shez-
BTW,你的输出是什么样的?你期望它看起来像什么?b $ b喜欢?你说的是即使它是< 0" ;.我没有看到任何数据更少
比0,是吗?
V
BTW, what does your output look like? What do you expect it to look
like? You speak of "even if it is < 0". I don''t see any data less
than 0, do you?
V
对不起。我用我的输入文件测试了你的程序,它按预期工作了
(忽略了如何处理0.0的上面的缺陷)
什么输入文件你在用吗?你觉得你的节目在哪里?
错了?
-
Karl Heinz Buchegger
这篇关于需要帮助!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!