char a[100];
gcvt(number,10,a);
cout<<"integer= "<<number<<" string="<<a<<endl;
return 0;
}
输出结果:
integer= 123457 string= 123456.67
#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;
int main()
{
std::string name("12345");
int age = 27;
stringstream os;
os << age;
os >> name;
// name:27
cout<<name<<endl;
return 0;
}
输出结果;
27
#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;
int main()
{
stringstream ss_stream;
char sz_buf[5];
ss_stream << 88888;
ss_stream >> sz_buf; // 直接将数输出到sz_buf字符数组中
for(int i=0;i<5;i)
cout<<sz_buf[i]<<" ";
cout<<endl;
return 0;
}
输出结果:
8 8 8 8 8
输入n个整数和一个正整数k(1<=k<=n),输出这些整数从小到大排序后的第k个(例如,k=1就是最小)。n<=10*e7
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
// 快速选择,平均时间复杂度O(N)
typedef int DataType;
#defineSWAP(x,y) {DataType t=x;x=y;y=t;}
intquickSelect( DataType* A, int beg, int end, int K )
{
DataType pivot;
int i, j;
if ( end - beg 1 < K ) // error
{
cout<<"error!"<<endl;
exit(0);
}
pivot = A[beg];
i = beg;
j = end 1;
// 将数组分为小于pivot和大于pivot的两部分
for ( ;; )
{
while(A[i] < pivot );
while(A[--j] > pivot );
if( i > j ) break;
SWAP(A[i], A[j] );
}
SWAP( A[beg], A[j] );
if ( j - beg == K-1 )// 如果小于pivot的数目刚好为K-1个,这返回该pivot
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-34960-23.html
没人敢惹吧