帮小弟我看看怎么使用quicksort等标准C排序函数
帮我看看如何使用quicksort等标准C排序函数
这是我修改后的代码,但是被要求不能使用vector和map等容器或摸板,那么怎么改呢,应该是可以使用quicksort等C标准函数吧
#include <iostream>
#include <string>
#include <map>
#include <fstream>
using namespace std;
bool ReadFile(char* cFileName, map <int,int> * AllReadMap)
{
AllReadMap-> clear();
ifstream infile;
int nTempValue=0;
int nStringLenght = 0;
char cString[127]; //buffer
char cTempChar[10]; //一个整数的字符串
string strMyString = " ";
string strTempString = " ";
infile.open(cFileName, ios::in);
if (!infile)
{
return false; //false
}
while(!infile.eof() )
{
infile.getline(cString,127); //读取一行字符到缓冲区
strMyString = cString; //赋值给临时变量等待处理
nStringLenght = strMyString.length();//取长度
for(int i=0;i <nStringLenght; ++i)
{
//
int nCommaNumber = strMyString.find( ", ");
memset(cTempChar, '\0 ',10);
strTempString = strncpy(cTempChar,strMyString.c_str
(),nCommaNumber);
nTempValue = atoi(cTempChar);
string::iterator Pos;
Pos = strMyString.begin();
for(int j =0; j <nCommaNumber+1; ++j)
{
++Pos;
}
AllReadMap-> insert(std::make_pair(nTempValue,nTempValue));
strMyString.erase(strMyString.begin(),Pos);
}
}
infile.close();
return true; //true
}
int main()
{
//////////////////////small to big
map <int,int> ValueMap;
ReadFile( "myfile01.txt ",&ValueMap);
map <int,int> ::iterator Pos;
for(Pos=ValueMap.begin();Pos!=ValueMap.end();Pos++)
{
cout < < Pos-> first < < endl;
}
return 0;
}
------解决方案--------------------
qsort看C库函数手册就应该会用了。
这是我修改后的代码,但是被要求不能使用vector和map等容器或摸板,那么怎么改呢,应该是可以使用quicksort等C标准函数吧
#include <iostream>
#include <string>
#include <map>
#include <fstream>
using namespace std;
bool ReadFile(char* cFileName, map <int,int> * AllReadMap)
{
AllReadMap-> clear();
ifstream infile;
int nTempValue=0;
int nStringLenght = 0;
char cString[127]; //buffer
char cTempChar[10]; //一个整数的字符串
string strMyString = " ";
string strTempString = " ";
infile.open(cFileName, ios::in);
if (!infile)
{
return false; //false
}
while(!infile.eof() )
{
infile.getline(cString,127); //读取一行字符到缓冲区
strMyString = cString; //赋值给临时变量等待处理
nStringLenght = strMyString.length();//取长度
for(int i=0;i <nStringLenght; ++i)
{
//
int nCommaNumber = strMyString.find( ", ");
memset(cTempChar, '\0 ',10);
strTempString = strncpy(cTempChar,strMyString.c_str
(),nCommaNumber);
nTempValue = atoi(cTempChar);
string::iterator Pos;
Pos = strMyString.begin();
for(int j =0; j <nCommaNumber+1; ++j)
{
++Pos;
}
AllReadMap-> insert(std::make_pair(nTempValue,nTempValue));
strMyString.erase(strMyString.begin(),Pos);
}
}
infile.close();
return true; //true
}
int main()
{
//////////////////////small to big
map <int,int> ValueMap;
ReadFile( "myfile01.txt ",&ValueMap);
map <int,int> ::iterator Pos;
for(Pos=ValueMap.begin();Pos!=ValueMap.end();Pos++)
{
cout < < Pos-> first < < endl;
}
return 0;
}
------解决方案--------------------
qsort看C库函数手册就应该会用了。