Accelerated C + + reading notes 1

Source: Internet
Author: User

The relevant C + + course listened to a lot, but lectures can not replace reading, book system summary for the formation of system, check leak fill still have a great effect.

There are a plethora of C + + books on the market, well-known as "C + + Primer", "the C + + programming language" and other comprehensive introduction of language features of the book and "Effective C + +" "more effective C + +" such as expert advice books. In order to correspond to their ongoing C + + series of learning, need a short period of C + + has a system of carding, that is, the use of 2-8 principles, in a short time, grasp the most commonly used part. Obviously the above mentioned two tome is not the first choice of the present. So another is a rare, but also well-recognized introductory book, "Accelerated C + +" may be the best choice (as the book says, if you want to use C + + programming, you need to master all the knowledge in this book, although it does not tell you want to know everything). In the next vacation this period of time, oneself need to read carefully and as much as possible to complete the book of the exercises, here began to summarize the post, hope to be able to comb the harvest to precipitate.

However, as Houtie teacher said, "as a good learner, the important thing about your learning attitude, the start of course can start from a casual sketch, but if the big wide berth, run away, face any technology just fast food, learning language and never write programs, that will never become a master of the day." "With their own encouragement, after the start of reading the C + + primer ...

No. 0 Chapter Introduction

To the Hello World program explanation comb, naturally will not have what new thing, related concept of review bar.

The first chapter uses the string

This chapter program:

//ask for a person ' s name, and generate a framed greeting#include <iostream>#include<string>intMain () {std::cout<<"Please enter your first name:"; STD::stringname; Std::cin>>name; //build the message that we intend to write    ConstSTD::stringGreeting ="Hello,"+ name +"!"; //Build the second and fourth lines of the output    ConstSTD::stringSpaces (Greeting.size (),' '); ConstSTD::stringSecond ="* "+ spaces +" *"; //Build the first and fifth lines of the output    ConstSTD::stringFirst (Second.size (),'*'); //Write it allStd::cout <<Std::endl; Std::cout<< First <<Std::endl; Std::cout<< Second <<Std::endl; Std::cout<<"* "<< Greeting <<" *"<<Std::endl; Std::cout<< Second <<Std::endl; Std::cout<< First <<Std::endl; return 0;}

1. wchar_t is used to store "wide character" built-in type, wide character is a large enough type, you can save characters for multiple languages.

2. The string class has a

String Z (n,c), which defines the string type variable z, and initializes z with n copies of the character C. Note: c must be a char type and cannot make a string type or string constant

3. In the string class plus s+t, either s or T can be (but not two) string literals or char types

So

std::string exclaim = "!";

std::string s = "Hello" + "world" + exclaim; Error, + left associative, two string constants added

Chapter II Cycle and Count

This chapter describes the use of statements such as while,if,for, the concept of loop invariant when designing while statements, as well as arithmetic, logical operators, and their precedence, new things are not much, see programs and comments

#include <iostream>#include<string>//say what standard-library names we useusingStd::cin;usingStd::endl;usingStd::cout;usingSTD::string;intMain () {//Ask for the person ' s namecout <<"Please enter your first name:"; //Read the name    stringname; CIN>>name; //build the message that we intend to write    Const stringGreeting ="Hello,"+ name +"!"; //The number of blanks surrounding the greeting    Const intPad =0; //The number of rows and columns to write    Const introws = Pad *2+3; Const string:: Size_type cols = greeting.size () + pad *2+2;//when you define a variable to hold the size of a particular data structure,//develop a good habit of using the appropriate types defined by the standard library. //size_t unsigned integer type (from header file <cstddef>), can contain any object length 
    //string::size_type unsigned integer type, containing any string object length//write a blank line to separate the output from the inputcout <<Endl; //write ' rows ' rows of output//Invariant:we has written ' r ' rows so far     for(intR =0; R! = rows; ++r) {string:: Size_type C =0; //Invariant:we has written ' C ' characters so far in the current row         while(c! =cols) {            //Is it time to write the greeting?            if(r = = Pad +1&& c = = Pad +1) {cout<<greeting; C+=greeting.size (); } Else {                //is we on the border?                if(r = =0|| r = = Rows-1||C==0|| c = = cols-1) cout<<"*"; Elsecout<<" "; ++C; }} cout<<Endl; }    return 0;}

Chapter III Using bulk Data

It is mainly the definition of vectors and related operations, as well as the introduction of flow output control.

1 header file <ios> defines the streamsize type, which is the type used by the input and output library to represent the length;

Header file <iomanip> defines the control setprecision, which describes how many significant digits you want the output to contain

2 vector

Size_type unsigned type, sufficient to store the maximum vector size;

V.begin (), V.end (), V.push_back, V.size (),

3 Other library functions

Sort (b,e), Max (E1,e2), while (cin>>x),

S.precision (n) sets the output accuracy of the stream s after N, (omitting n to maintain the original output accuracy) returns the original output precision

Setprecision (N)

#include <algorithm>#include<iomanip>#ifndef __gnuc__#include<ios>#endif#include<iostream>#include<string>#include<vector>usingStd::cin;usingStd::sort;usingStd::cout;usingstd::streamsize;usingStd::endl;usingSTD::string;usingStd::setprecision;usingstd::vector;intMain () {//ask for and read the student ' s namecout <<"Please enter your first name:"; stringname; CIN>>name; cout<<"Hello,"<< name <<"!"<<Endl; //ask for and read the midterm and final gradescout <<"Please enter your midterm and final exam grades:"; Doublemidterm, Final; CIN>> Midterm >>final; //ask for and read the homework gradescout <<"Enter All your homework grades,"            "followed by end-of-file:"; Vector<Double>homework; Doublex; //invariant: ' Homework ' contains all the homework grades read so far     while(Cin >>x) homework.push_back (x); //Check that the student entered some homework grades#ifdef _msc_ver typedef std::vector<Double>:: Size_type VEC_SZ;#elsetypedef vector<Double>:: Size_type VEC_SZ;#endifVEC_SZ Size=homework.size (); if(Size = =0) {cout<< Endl <<"You must enter your grades. "                        "Please try again."<<Endl; return 1; }    //sort the gradessort (Homework.begin (), Homework.end ()); //Compute the median homework gradeVEC_SZ mid = size/2; Doublemedian; Median= size%2==0? (Homework[mid] + homework[mid-1]) /2: Homework[mid]; //compute and write the final gradeStreamsize Prec =cout.precision (); cout<<"Your final Grade is"<< Setprecision (3)         <<0.2* Midterm +0.4* Final +0.4*Median<< setprecision (PREC) <<Endl; return 0;}

Accelerated C + + reading notes 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.