首页 > 代码库 > C++学习笔记
C++学习笔记
文件读写操作
#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;}
C++学习笔记
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。