Essential C++ 之 编程基础

(1)
常识:page 1 - 25

布尔值:
字符:
整数:
浮点数:算数运算符:
关联运算符:
逻辑运算符:
等号运算符:
小于等于符:
赋值运算符:
累加运算符:
条件运算符:
复合运算符:

条件分支:
循环控制:
抽象化程序库:字符串,向量vector

示例:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string user_name;
    cout << "Please enter your name: ";
    cin << '\n'
        << "Hello, "
        << user_name
        << " ...and goodbye!\n";
    return 0;
}

namespace :命名空间
初始化 :

1.int a = 0; //源自c语言
2.int a(0); //构造函数语法

array和vector的用法
vector是个class template

#include<vector>
vector<int> elem_seq(seq_size);
elem_seq[0] = 1;
elem_seq[1] = 2;
//……初始化的一种方法
//-------------------
int elem_val[seq_size] = {……};//array初始化
vector<int> elem_seq{elem_val , elem_val + seq_size}; 
//用array的值来初始化elem_seq
elem_seq.size()返回vector的大小
#include <iostream>
#include <cstdlib>//

using namespace std;
const int seq_cnt = 6;

vector<int> *seq_addre[ seq_cnt ] = {
&fibonacci,&lucas,&pell,
&triangular,&square,&pentagonal
};

vector<int> *current_vec = 0;
for (int ix = 0;ix < seq_cnt; ++ix)
{
    current_vec = seq_cnt[ix];
}

文件的读写:
头文件:fstream

#include <fstream>
ofstream outfile("seq_data.txt");//供输出用的对象
//追加模式是 ofstream outfile("seq_data.txt" , ios_base::app);
//追加模式是原文件已经存在,并且不希望丢弃原来的内容

if( ! outfile) //判别文件是否成功开启
    //未能开启,写入cerr,标准错误输出设备,立即显示于终端上,无缓存
    cerr << "Oops!Unable to save session data!\n";
else
    //开启成功,将信息写入
    outfile << usr_name << ' '
            << num_tries << ' '
            << num_right << endl;
//endl插入一个换行字符,并清除输出缓冲区的内容
//开启一个可供读取的文件,可定义ifstream对象
ifstream infile("seq_data.txt");

int num_tries = 0;
int num_cor = 0;

if (! infile)
{
    //由于某种原因,文件无法开启
    //我们将假设这是一个新的用户
}
else
{
    //ok,读取文件中的每一行
    //检查用户是否曾经用过这个程序
    //每一行的格式是:
    //name   num_tries   num_correct
    // Anna      24          19  
    //  ……
    // nt:猜过的总次数(num_tries)
    // nc:才对的总次数(num_correct)

    string name;
    int nt;
    int nc;
    while (infile >> name )//每次迭代读取文件下一行,直到文件尾端
    {
        infile >> nt >> nc;
        if (name == usr_name )
        {
            cout << "Welcome back, " << usr_name
                 << "\nYour current score is " << nc
                 << " out of " << nt << "\nGood Luck!\n";
            num_tries = nt;
            num_cor = nc;
        }   
    }
}
//同时读写同一个文件,定义一个fstream对象,为了以追加模式开启,
//掺入第二个参数值 ios_base::in|ios_base::app
fstream iofile("seq_data.txt",
                ios_base::in|ios_base::app );
if (! iofile )
//由于某种原因,文件无法开启
else
{
    //开始读取之前,将文件重新定位至起始处
    iofile.seekg( 0 );
    //当以附加模式来开启文件时,文件位置会位于尾端,如果没有重新定位
    //那么立刻就会遇上“读到文件尾”的状况,seekg()可将文件位置重新定位到起始处

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值