C + + Primer Learning Note 1-7

Source: Internet
Author: User
Tags bitset

The school only taught C and java,c++ is their own view, always still like the C style.
Many of the features of C + + are always smattering, and perhaps you can learn some details.

The first chapter Quick Start
CIN (see-in) standard input
cout (see-out) standard output
Cerr (SEE-ERR) standard error
Clog
The following 3 specific differences in implementation:
The output of cout,cerr and clog can be redirected, such as files, by differences between 1> and 2>.
The outputs of the cout and clog are buffers, the buffers are full, or the Endl,cerr direct output is encountered.
For example, in the code cout and Cerr, clog, and then execute the file >1.txt, then cout content arrives 1.txt,cerr, clog content to the screen.
If you use 2>, which is the error output, the result is reversed.
#include <iostream>
using namespace Std;
int main ()
{
cout<< "cout" <<endl;
cerr<< "Cerr" <<endl;
clog<< "Clog" <<endl;
return 0;
}

Run g++ test.cpp
./a.out so all output screen
./a.out > 1.txt so the screen output cerr and clog, the text is cout
./a.out 2>1.txt? Screen output cout, text is cerr and clog


CIN also has a return value.
can use
int value;
while (Cin>>value) ...;
To enter a value, or false if it is not valid or EOF.

(On these lines, re-dozen several times, QQ led to the death of 2 times, roaming mysteriously quit once)

Chapter II variables and basic types
Initialization points: Copy initialization and direct initialization.
int ival=1000;
int ival (1000);

String All_nines (9 ');//all_nines= "9999999999"

The flow is better than printf, that is, the string output is not annoying. C_STR (). Whether it's CIN or cout.

const int connum=0; Defines a constant that must be initialized, used to be defined by a macro, which is used only when the method passes a parameter, but always does not feel that the const is better than the macro definition when defining a constant .

Variables defined in the file, the non-const variable defaults to extern, and if you add a const, the constant cannot be used outside of this file. unless extern const int connum = 0;

The enumeration is four bytes long, each member is of type int and can initialize negative numbers.

Either C or c++,struct is the default implementation = number assignment.

Chapter III Standard Library types
Cin ignores the opening whitespace character (Space wrapping tab). Read terminates when a whitespace character is encountered.
To read a row with Getline, ignore the last carriage return.
String line;
while (Getline (Cin,line))
cout<<line<<endl;
cout<< "Ctrl + D" <<endl;
CTRL + D ends the input.

The string method of the size () return type is String::size_type, unsigned integer.
A variable of string can be manipulated directly in the form of an array, including substitution. such as String str1= "123"; str1[1]=a;//str1= "1A3"

P89 Page Table 3-3 records the functions of judging and manipulating letters. Or look here:
Http://net.pku.edu.cn/~yhf/linux_c/function/01.html#linuxc1

Replace case
For (Size_type i=0;i!=s.size (); ++i) S[i]=tolower (S[i]);
In fact, it can also be written like this:
Transform (S.begin (), S.end (), S.begin (), tolower);
It is said that the greatest advantage of using STL is to discard the for loop.

Vector<int> Ivec (10,-1)//10 members, each of which is-1. No second parameter, default to 0
Vector<string> Svec (x, "HI")//10 members, each initialization assignment hi

The iterator iterator is actually a pointer, but notice that the iterator's + + or--cannot operate on the iterator inside the incoming parameter, or it will go wrong.

Also provides a const_iterator, the function only the value can not be assigned value.
There is a need to differentiate the const iterator, which can be assigned but not + + or--。

Vector<int>::const_iterator it = Vector1.begin ();
*it=5;//error, cannot assign value

Const Vector<int>::iterator IT = Vector1.begin ();
*it =5;//Correct
it++;//error, not + +

Iterators can use +n, or-n operations. Like what:
Vector<int>::iterator mid = Vi.begin () + vi.size ()/2;


Standard library Bitset types
I've never used it.

The difference from a vector is in the length of the angle brackets rather than the type.
Units are bits. Bitset<32>bitvec (0xFFFF) is 65536. 0 to 15 bits are 1,16 to 31 bits is 0.
You can also initialize with a string:
String str ("11100111"); Bitset<32>bitves (str,5,3)//from the 5th place to take 3 values, then is 0x111.
Cout<<bitves.to_ulong () <<endl; Output 7
cout<<bitves<<endl;//Output 00000000000000000000000000000111

P90 is the method of Bitset.

Fourth chapter arrays and pointers
Whether an array or a variable, defined outside of the function is automatically initialized 0, defined in the function is a random value.
If the member is a class, no matter where it is defined, the default constructor is called automatically.

Char a1[]={' C ', ' + ', ' + '};//print A1, print out the back of the garbled, until there are 0 appear.

const int *A1;//*A1 cannot be modified. A1 can be modified.
int *const a1;//If the pointer is not a constant, then *A1 can be modified. A1 can not be modified.
const int *const A1;//*A1 and A1 cannot be modified.

There's a lot of people who are going to make mistakes.
typedef string *PSTR;
Const PSTR CStr;
So what kind of cstr is it?
Const string* cstr;//Error!
String *const cstr;//is correct.

Watch out.

The initialization of a dynamic array can only use the default value of 0, such as:
int *pai = new INT[10] ();//All values are 0, parentheses cannot give values, otherwise compile.

Creating an array of 0 elements is not legal. However, creating a dynamic array of 0 elements is legal.
int A[0];//error
int *a = new Int[0];//ok

Fifth Chapter Expressions
&& priority is higher than | |

The difference between ++i and i++:
The previous code style has already been said, the Post + + will copy more copies of the object out, increase the cost. However, there is no difference between the underlying types, because the compiler does optimizations, but there is an additional overhead for complex types.

If the program runs out of memory, new may fail, and the system throws an Bad_alloc exception instead of returning null!
However, running out of memory can still be successful until the address space is exhausted.

A pointer of delete 0 is a legitimate operation and does not give an error.

Forced Reload:
Static_cast,dynamic_cast,const_cast,reinterpret_cast
Really do not like to use, the direct use of C's forced conversion more convenient, but also do not see any benefits .
The cast of C is equivalent to reinterpret_cast.

Sixth Chapter Statement
int i=0;
A[i]=b[i++];
The result is a[0]=b[0];i++;


Throws an exception, which is to find the processing statement from inside out.

is not processed to the Terminate standard library function, general processing is to exit the program.

p189 table 6-1 defines various exceptions.
<exception> includes the most common exception classes.
<stdexcept> defines several common exceptions, such as rumtime_error when used, such as throw Runtime_error ("XXX");
<new>new throws a Bad_alloc exception when it fails, instead of returning null.
<type_info> defines the bad_cast

try{
......
Throw Runtime_error ("XXX");
}
catch (Runtime_error err)
{
Cout<<err.what () <<endl;
}
Output XXX.
If you do not catch this exception, show the exception class why, and then automatically print what content, and finally quit the program.
If you think every exception to write, write a line, but XXX is not. Like what:
catch (Exception err)
{
Cout<<err.what () <<endl;
}
No matter what exception is thrown, the result of printing is st9exception
I wonder if there is any way to know what exception is thrown.
Or:
catch (...)
{
}
Don't deal with anything.

The same while can also use multiple conditions, such as while (I<5, j<6) {}

Compile the generated macro inside the processor, which can be used in the code. I used to think of VC stuff because of the standard C stuff.
__file__ file name
__line__ the line number of the row
__time__ compile time
__date__ Compile Date

Seventh Chapter function

Inline functions, like macro definitions, are automatically replaced with the contents of the function at compile time, avoiding the additional execution overhead of writing functions.

The definition of the inline function needs to be written in the header file.
Or like a macro definition.


This article is from "Flying Justice Blog" blog, please be sure to keep this source http://xzq2000.blog.51cto.com/2487359/1766858

C + + Primer Learning Note 1-7

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.