zoukankan      html  css  js  c++  java
  • 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("
    ",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] != '')
    	{
    		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]='';
    	return reverse(ch);
    }
    
    char* reverse(char* ch)
    {
    	int size = 0;
    	while(ch[size]!='')
    	{
    		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;
    }
    

      

  • 相关阅读:
    流程图制作在云上 https://www.processon.com/
    白板编程浅谈——Why, What, How
    如何创建一个非常酷的3D效果菜单
    Xcode及模拟器SDK下载
    Swift项目兼容Objective-C问题汇总
    iOS 多个精致动画
    代码注释中的5要与3不要
    如何处理iOS中照片的方向
    会报编译器警告的Xcode 6.3新特性:Nullability Annotations
    iOS应用架构谈 view层的组织和调用方案
  • 原文地址:https://www.cnblogs.com/qxzy/p/4133612.html
Copyright © 2011-2022 走看看