为什么你一开始的示例数据中是浮点类型,这次又都是整型?

程序代码:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main( void )
{
// 打开文件
ifstream fin( "d:\\a.txt" );
if( !fin )
return 1;
vector<double> sb, sd;
{
// 读入数据
string a, c;
for( double b,d; fin>>a>>b>>c>>d; )
{
sb.push_back(b);
sd.push_back(d);
}
// 排序
sort( sb.begin(), sb.end() );
sort( sd.begin(), sd.end() );
}
// 输出
cout << "array1 = ";
copy( sb.begin(), sb.end(), ostream_iterator<double>(cout," ") );
cout << '\n';
cout << "array2 = ";
copy( sd.begin(), sd.end(), ostream_iterator<double>(cout," ") );
cout << '\n';
return 0;
}
假如你觉得那两个string没必要浪费空间读取,可以

程序代码:
#include <iostream>
#include <fstream>
#include <vector>
#include <limits>
#include <algorithm>
#include <iterator>
using namespace std;
int main( void )
{
// 打开文件
ifstream fin( "d:\\a.txt" );
if( !fin )
return 1;
// 读入数据
vector<double> sb, sd;
{
for( double b,d; ((fin>>ws).ignore(numeric_limits<streamsize>::max(),' ')>>ws>>b>>ws).ignore(numeric_limits<streamsize>::max(),' ')>>d; )
{
sb.push_back(b);
sd.push_back(d);
}
// 排序
sort( sb.begin(), sb.end() );
sort( sd.begin(), sd.end() );
}
// 输出
cout << "array1 = ";
copy( sb.begin(), sb.end(), ostream_iterator<double>(cout," ") );
cout << '\n';
cout << "array2 = ";
copy( sd.begin(), sd.end(), ostream_iterator<double>(cout," ") );
cout << '\n';
return 0;
}