0_introduction_cpp_prime

这篇博客介绍了C++的基础知识,包括iostream库用于标准输入输出,如cin和cout,以及endl操纵符。文章详细讲解了while、for循环的语法和用法,并展示了如何处理文件输入直到结束。此外,还讨论了编译器的工作原理,如错误检测和条件判断。最后,提到了缓冲区的概念及其在输入输出中的作用。
摘要由CSDN通过智能技术生成

1. in & out

iostream: standard library
istream & ostream
cin; cout; cerr; clog

#include <iostream>
int main()
{
    std::cout << "enter two numbers:" << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << " the sum of " << v1 << " and " << v2 << " is " << v1+v2 << std::endl;
    return 0;

}

<< & >> oprator

  • two operands
  • ostream object << the value needs to print
  • << writes the value into the ostream object (write the right thing into the left thing)
  • ( std::cout << “enter two numbers:” ) << std::endl
  • “enter two numbers:” is a string literal
  • endl: a manipulator; to end this line; to ensure the content in the buffer is refreshed into the device; more about buffer

namespace

std::cout & std:endl

  • the cout is defined in the std namespace
  • to avoid duplicate name
  • :: scope operator

The standard library defines different << and >> for different types of variables.

2. comments

one line comments: //
multiple lines comments: /* */

  • comments cannot be nested

3. flow of control

3.1 while

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

grammar:

while (condition)
	statement
  • block: multiple statements included in a curly bracket
  • +=: compound assignment operator
  • ++: prefix increment operator

3.2 for

#include <iostream>
int main()
{
    int sum=0;
    for (int val=1; val<=10; ++val)
        sum += val;
    std::cout << "sum is " << sum << std::endl;
    return 0;
}

** grammar**

for (init-statement; condition; expression)

3.3

#include <iostream>
int main()
{
    int sum = 0, value = 0;
    // read until end-of-file, calculating a running total of all values read
    while (std::cin >> value)
        sum += value; // equivalent to sum = sum + value
        std::cout << "Sum is: " << sum << std::endl;
    return 0;
}
  • istream is a condiftion; to test the state of a stream
  • invalid state of a stream: hit end-of-file signal / invalid input

3.4 complier

one of its job is to detect error (in format)

  • syntax error; type error; declaration error;

3.5 if

#include <iostream>
int main()
{
    int currVal = 0, val = 0;
    
    if (std::cin >> currVal) { //read one data and make sure there is data avaliable
        int cnt = 1;
        while (std::cin >> val){ // read remaining data
            if (val == currVal)
                ++cnt;
            else {
                std::cout << currVal << " occurs "
                << cnt << " times " << std::endl;
                currVal = val;
                cnt = 1;
            }
        }
        std::cout << currVal << " occurs " << cnt << " times " << std::endl;
    }
}

4.

buffer A region of storage used to hold data. IO facilities often store input (or output) in a buffer and read or write the buffer independently from actions in the program. Output buffers can be explicitly flushed to force the buffer to be written. By default, reading cin flushes cout; cout is also flushed when the program ends normally.

member function Operation defined by a class. Member functions ordinarily are
called to operate on a specific object.

method Synonym for member function.

extra video How C++ Works

  • main function returns 0 by default (only main)
  • compile: turn textin programming languages into a binary execuable file for computer to recognize
  • compile each cpp file individually into object file, the linker works to stitch each obj file into one exe file

extra viedo How the C++ Compiler Works

  • complie: cpp file -> obj file; for cpp, no definition of ‘file’, file is only the source to provide code to compile
  • cpp file != translation unit
  • pre-process stage: the compiler go through all the pre-processing statements (include, defice, f, etc.)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值