`
sai_ruby
  • 浏览: 21800 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

C++笔记(1)

阅读更多
stringstream allows a string-based object to be treated as a stream. This way we can perform extraction or insertion operationgs from/to strings, which is especially useful to convert strings to numerical values and vice versa.
e.g.
string mystr("1024");
int myint;
stringstream(mystr)>>myint;

Using this method, instead of direct extractions of integer values, we have more control over what happens with the input of numeric values from the user, since we are separating the process of obtaining input from the user (we now simply ask for lines) with the interpretation of that input. Therefore, this method is usually preferred to get numerical values from the user in all programs that are intensive in user input.

翻译一下大概意思就是:把用户输入与输入内容的解析分离,有利于对输入的控制。

exit is a function defined in the cstdlib library.
The purpose of exit is to terminate the current program with a specific exit code. Its prototype is:
void exit (int exitcode);
The exitcode is used by some operating systems and may be used by calling programs. By convention, an exit code of 0 means that the program finished normally and any other value means that some error or unexpected results happened.

Passing by reference is also an effective way to allow a function to return more than one value.

When declaring a function we can specify a default value for each of the last parameters.If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is ignored and the passed value is used instead.

Notice that a function cannot be overloaded only by its return type. At least one of its parameters must have a different type.

inline function
The inline specifier indicates the compiler that inline substitution is preferred to the usual function call mechanism for a specific function. This does not change the behavior of a function itself, but is used to suggest to the compiler that the code generated by the function body is inserted at each point the function is called, instead of being inserted only once and perform a regular call to it, which generally involves some additional overhead in running time.
内联函数的执行效率比一般函数调用效率高。

char myword [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char myword [] = "hello";
In both cases the array of characters myword is declared with a size of 6 elements of type char: the 5 characters that compose the word "Hello" plus a final null character ('\0') which specifies the end of the sequence and that,in the second case, when using double quotes (") it is appended automatically.
notice that we are talking about initializing an array of characters in the moment it is being declared, and not about assigning values to them once they have already been declared. In fact because this type of null-terminated arrays of characters are regular arrays we have the same restrictions that we have with any other array, so we are not able to copy blocks of data with an assignment operation

& is the reference operator and can be read as "address of"
* is the dereference operator and can be read as "value pointed by"

an array is just like a constant pointer

To conduct arithmetical operations on pointers is a little different than to  conduct them on regular integer data types. To begin with, only addition and subtraction operations are allowed to be conducted with them, the others
make no sense in the world of pointers. But both addition and subtraction have a different behavior with pointers according to the size of the data type to which they point.

void pointers
This allows void pointers to point to any data type, from an integer value or a float to a string of characters. But in exchange they have a great limitation: the data pointed by them cannot be directly dereferenced (which is logical, since we have no type to dereference to), and for that reason we will always have to cast the address in the void pointer to some other pointer type that points to a concrete data type before dereferencing it.

C++ allows operations with pointers to functions. The typical use of this is for passing a function as an argument to another function, since these cannot be passed dereferenced.

*p++ = *q++;
Because ++ has a higher precedence than *, both p and q are increased, but because both increase operators (++) are used as postfix and not prefix, the value ssigned to *p is *q before both p and q are increased. And then both are increased. It would be roughly equivalent to:
*p = *q;
++p;
++q;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics