64,651
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
vector<double> v1, v2, v3;
string line;
int lineno = 0;
char *stop;
ifstream infile("data.txt");
while (getline(infile, line))
{
switch (lineno) {
case 0: v1.push_back(strtod(line.c_str(), &stop)); break;
case 1: v2.push_back(strtod(line.c_str(), &stop)); break;
case 2: v3.push_back(strtod(line.c_str(), &stop)); break;
}
lineno++;
}
// result = v1 + v2
vector<double> result(v1.size(), 0 );
transform(v1.begin(), v1.end(), v2.begin(), result.begin(), plus<double>());
// print:
// v1
// +
// v2
// =
// result
//
vector<double>::const_iterator it;
for (it = v1.begin(); it != v1.end(); it++)
cout << *it << endl;
cout << "+" << endl;
for (it = v2.begin(); it != v2.end(); it++)
cout << *it << endl;
cout << "=" << endl;
for (it = result.begin(); it != result.end(); it++)
cout << *it << endl;
return 0;
}
[/quote]
不行啊。。。回来我自己运行后发现只能显示读取第一个数字啊#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> v1, v2, v3;
string line;
int lineno = 0;
ifstream infile("data.txt");
while (getline(infile, line))
{
switch (lineno) {
case 0: v1.push_back(line); break;
case 1: v2.push_back(line); break;
case 2: v3.push_back(line); break;
}
lineno++;
}
return 0;
}
供参考。#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector<int> v1,v2,v3;
string temp;
ifstream fin("data.txt");
while(getline(fin,temp))
{
istringstream temp1(temp);
while(temp1 >> temp)
{
v1.push_back(temp);
}
}
return 0;
}
上面只是一种情况,你可以试下其他的#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
vector<double> v1, v2, v3;
string line;
int lineno = 0;
char *stop;
ifstream infile("data.txt");
while (getline(infile, line))
{
switch (lineno) {
case 0: v1.push_back(strtod(line.c_str(), &stop)); break;
case 1: v2.push_back(strtod(line.c_str(), &stop)); break;
case 2: v3.push_back(strtod(line.c_str(), &stop)); break;
}
lineno++;
}
// result = v1 + v2
vector<double> result(v1.size(), 0 );
transform(v1.begin(), v1.end(), v2.begin(), result.begin(), plus<double>());
// print:
// v1
// +
// v2
// =
// result
//
vector<double>::const_iterator it;
for (it = v1.begin(); it != v1.end(); it++)
cout << *it << endl;
cout << "+" << endl;
for (it = v2.begin(); it != v2.end(); it++)
cout << *it << endl;
cout << "=" << endl;
for (it = result.begin(); it != result.end(); it++)
cout << *it << endl;
return 0;
}
[/quote]
对的!多谢了,,
v1.push_back(strtod(line.c_str(), &stop))
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
vector<double> v1, v2, v3;
string line;
int lineno = 0;
char *stop;
ifstream infile("data.txt");
while (getline(infile, line))
{
switch (lineno) {
case 0: v1.push_back(strtod(line.c_str(), &stop)); break;
case 1: v2.push_back(strtod(line.c_str(), &stop)); break;
case 2: v3.push_back(strtod(line.c_str(), &stop)); break;
}
lineno++;
}
// result = v1 + v2
vector<double> result(v1.size(), 0 );
transform(v1.begin(), v1.end(), v2.begin(), result.begin(), plus<double>());
// print:
// v1
// +
// v2
// =
// result
//
vector<double>::const_iterator it;
for (it = v1.begin(); it != v1.end(); it++)
cout << *it << endl;
cout << "+" << endl;
for (it = v2.begin(); it != v2.end(); it++)
cout << *it << endl;
cout << "=" << endl;
for (it = result.begin(); it != result.end(); it++)
cout << *it << endl;
return 0;
}
double d = std::strtod(v1[0].c_str(), &stop);
是这个意思吗?
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> v1, v2, v3;
string line;
int lineno = 0;
ifstream infile("data.txt");
while (getline(infile, line))
{
switch (lineno) {
case 0: v1.push_back(line); break;
case 1: v2.push_back(line); break;
case 2: v3.push_back(line); break;
}
lineno++;
}
char *stop;
cout << std::strtod(v1[0].c_str(), &stop) << endl;
cout << std::strtod(v2[0].c_str(), &stop) << endl;
cout << std::strtod(v3[0].c_str(), &stop) << endl;
return 0;
}
data.txt:
123.65
1241231.23
895480305.08
输出:
123.65
1.24123e+006
8.9548e+008
[/quote]
多谢,,我想知道能不能直接读取v1的数据用于计算,比如说计算v1 + v2如果v1,v2长度相同的话#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> v1, v2, v3;
string line;
int lineno = 0;
ifstream infile("data.txt");
while (getline(infile, line))
{
switch (lineno) {
case 0: v1.push_back(line); break;
case 1: v2.push_back(line); break;
case 2: v3.push_back(line); break;
}
lineno++;
}
char *stop;
cout << std::strtod(v1[0].c_str(), &stop) << endl;
cout << std::strtod(v2[0].c_str(), &stop) << endl;
cout << std::strtod(v3[0].c_str(), &stop) << endl;
return 0;
}
data.txt:
123.65
1241231.23
895480305.08
输出:
123.65
1.24123e+006
8.9548e+008