标签:
文件读写操作
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void main(int argc, char* argv[])
{
ifstream in("C:\\Users\\Administrator\\Desktop\\C++.txt",ifstream::out);
ofstream out("C:\\Users\\Administrator\\Desktop\\CC.txt",ifstream::app);
if(!in)
{
cout<<"Cannot open input file!"<<endl;
}
string str;
while(getline(in,str))
{
out.write(str.c_str(),str.size());
out.write("\n",1);
}
}
atoi和itoa实现:
#include <iostream>
using namespace std;
int atoi(char* ch);
char* itoa(int n,char* ch);
char* reverse(char* ch);
void main(int argc, char* argv[])
{
char* ch = "123579";
int a = atoi(ch);
cout<<a<<endl;
int n = 752456;
char cc[10];
char* c = itoa(n,cc);
cout<<c<<endl;
}
int atoi(char* ch)
{
int i = 0;
int num = 0;
while (ch[i] != ‘\0‘)
{
num = num * 10 + ch[i]- ‘0‘;
i++;
}
return num;
}
char* itoa(int n,char* ch)
{
int i = 0;
while(n)
{
ch[i++] = n % 10 + ‘0‘;
n = n / 10;
}
ch[i]=‘\0‘;
return reverse(ch);
}
char* reverse(char* ch)
{
int size = 0;
while(ch[size]!=‘\0‘)
{
size++;
}
char temp;
for(int i = 0, j = size - 1; i < j; i++,j--)
{
temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
}
return ch;
}
读取一组整数,判断某个值的个数
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void main(int argc, char* argv[])
{
int num;
vector<int> nums;
while(cin>>num)
{
if(num==-1) break;
nums.push_back(num);
}
int number = 5;
int times = 0;
vector<int>::const_iterator result = find(nums.cbegin(),nums.cend(),number);
while(result != nums.cend())
{
times++;
result++;
result = find(result,nums.cend(),number);
}
cout<<"Total Times of 5 is: "<<times<<endl;
}
标签:
原文地址:http://www.cnblogs.com/qxzy/p/4133612.html