c++ primer概念整理 --第一章 开始写程序

来源:互联网 发布:网上买零食 知乎 编辑:程序博客网 时间:2024/04/28 05:30

在此声明,只整理盲点,所以不一定适合你

c++程序

.内置类型
.语言自身定义的类型.

.编译方法
linux: g++ - o prog prog.cc
VS: cl /EHsc prog.cpp

输入输出

.cin cout标准输入输出
.cerr 输出警告和错误信息,也叫标准错误.
.clog 输出程序运行时的一般信息
.endl 技术当前行并将与设备关联的缓冲区中的内容刷到设备中.

注释

.单行注释
.界定符注释(注释界定符不能嵌套)

控制流

.while语句
.for语句

读取不定量的数据

example:

#include<iostream>int main(){    int sum =0 , value =0 ;    while(std::cin>> value)        sum+=value;    std::cout <<"Sum is "<<sum<<std::endl;    return 0;}

.循环结束的条件是遇到文件结束符(end-of-file)键盘读入数据时,Windows输入结束符的方式是Ctrl+z然后回车,Unix中是Ctrl+D.

概念

.参数:向函数传递的值
.赋值:抹去一个对象的当前值,用一个新值取代.
.缓冲区:一耳光存储区域,用于保存数据,IO设施通常将输入或输出数据保存在一个缓冲区中,我们可以显示的刷新缓冲区,强制将缓冲区数据写入设备.默认情况下cin会刷新cout;程序非正常终止也会刷新cout.
.:一种用于用于定义自己的数据结构及其相关操作性的机制.类是c++中最近本的特性之一.
.类类型:类定义的类型.
.数据结构:数据及其所允许操作的一种逻辑的组合.
.表达式:最小的计算单元
.函数:具名的计算单元
.初始化:在一个对象创建的时候给予它一个值
.成员函数或方法:类定义的操作.
.命名空间:将库的名字单独放在单一位置的机制.
.变量:具名对象

程序

程序1:简单输入与统计当前重复程序

#include <iostream>int main(){    // currVal is the number we're counting; we'll read new values into val    int currVal = 0, val = 0;    // read first number and ensure that we have data to process    if (std::cin >> currVal) {                int cnt = 1;  // store the count for the current value we're processing        while (std::cin >> val) { // read the remaining numbers             if (val == currVal)   // if the values are the same                ++cnt;            // add 1 to cnt             else { // otherwise, print the count for the previous value                std::cout << currVal << " occurs "                           << cnt << " times" << std::endl;                currVal = val;    // remember the new value                cnt = 1;          // reset the counter            }        }  // while loop ends here        // remember to print the count for the last value in the file        std::cout << currVal << " occurs "                   << cnt << " times" << std::endl;    } // outermost if statement ends here    return 0;}

.

程序2: sales_item类定义的行为及其运用

int main() {    Sales_item total; // variable to hold data for the next transaction    // read the first transaction and ensure that there are data to process    if (std::cin >> total) {        Sales_item trans; // variable to hold the running sum        // read and process the remaining transactions        while (std::cin >> trans) {            // if we're still processing the same book            if (total.isbn() == trans.isbn())                 total += trans; // update the running total             else {                              // print results for the previous book                 std::cout << total << std::endl;                  total = trans;  // total now refers to the next book            }        }        std::cout << total << std::endl; // print the last transaction    } else {        // no input! warn the user        std::cerr << "No data?!" << std::endl;        return -1;  // indicate failure    }    return 0;}

.书店程序定义了很多的操作:
.提取isbn的书号
.重载输入输出的操作符
.重载赋值运算符
.重载加法运算符
.重载复合赋值运算符

.这些都是后续章节的重点展开内容,此处提及.

程序3 简单累加和

#include <iostream>int main(){    int sum = 0, val = 1;    // keep executing the while as long as val is less than or equal to 10    while (val <= 10) {        sum += val;  // assigns sum + val to sum        ++val;       // add 1 to val    }    std::cout << "Sum of 1 to 10 inclusive is "               << sum << std::endl;    return 0;}

程序4:for循环用例

#include <iostream>int main(){    int sum = 0;    for (int i = -100; i <= 100; ++i)        sum += i;    std::cout << sum << std::endl;    return 0;}

.练习题精选:
for循环与while循环各有什么优缺点.
.知道循环次数的话for循环更简洁,否则用while循环,循环体中的操作也可能导致判定条件的变化.

.掌握此章基础和规范

0 0
原创粉丝点击