Notes on C++ Primer

From Zheyong Fan
Jump to navigation Jump to search

Chapter 1

Chapter 2

  • const expression can substitute macro constant. Instead of using
#define PI 3.141592653589793

one can use

const double PI = 3.141592653589793;

and put it in a header file. It can be used in functions directly. I will do this for my codes in the future.


  • const objects are local to a file by default.
    • example for non-const objects:
 // f1.cpp
 int x = 10;

 // main.cpp
 #include <iostream>
 extern int x;
 int main(void)
 {
     std::cout << "x = " << x <<std::endl;
     return 0;
 }

Compile with

g++ main.cpp f1.cpp

and run with

./a.out

gives the output

x = 11
    • The following program can not compile:
 // f1.cpp
 const int x = 10;

 // main.cpp
 #include <iostream>
 extern const int x;
 int main(void)
 {
     std::cout << "x = " << x <<std::endl;
     return 0;
 }
    • To correct the above program, just add an extern in the file f1.cpp:
// f1.cpp
extern const int x = 10;


  • const reference can be assigned a non-const value, but not vice versa
    • This is ok:
#include <iostream>

int main(void)
{
    int x = 10;
    const int &y = x;
    std::cout << "y = " << y <<std::endl;
    return 0;
}

The output I got is:

y = 10
    • This is not ok:
#include <iostream>
int main(void)
{
    const int x = 10;
    int &y = x;
    std::cout << "y = " << y <<std::endl;
    return 0;
}

The compiling error I got is:

 binding ‘const int’ to reference of type ‘int&’ discards qualifiers
  • const reference can be assigned an R-value, but this is not true for non-const reference*
    • This is ok:
#include <iostream>

int main(void)
{
    int x = 10;
    const int &y = x + 1;
    std::cout << "y = " << y <<std::endl;
    return 0;
}

The output is

y = 11
    • This is not ok:
#include <iostream>

int main(void)
{
    int x = 10;
    int &y = x + 1;
    std::cout << "y = " << y <<std::endl;
    return 0;
}

The compiling error I got is:

 invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’

Chapter 3

Namespace using declarations

  • To declare individual names in a namespace, write something like:
using std::cin;
using std::cout;
using std::endl;
  • To declare all the names in a namespace, write something like:
using namespace std;

This is not recommended.

  • Headers should not include using declarations.


Library string type

  • A string is a variable-length sequence of characters.
  • To use the string type, one must include the <string> header:
#include <string>

Defining and initializing strings

  • Various ways to initialize a string object:
string s1; // s1 is an empty string
string s2 = "hello"; // copy initialization from a string literal
string s3("world"); // direct initialization from a string literal
string s4(4, 'a'); // s4 consists of 4 characters of 'a'
string s5 = s4; // copy initialization from another string
string s6(s5); // direct initialization from another string

Operations on strings

  • string operations
is >> s1       // Reads whitespace-separated string from input stream is into s; returns is.
os << s1       // Writes s onto output stream os; returns os.
getline(is, s) // Reads a line of input from is into s; return is.
s.empty()      // Returns true if s is empty; otherwise returns false.
s.size()       // Returns the number of characters in s, which is of type string::size_type.
s[n]           // Returns a reference to the char at position n in s.
s1 + s2        // Returns a string that is the concatenation of s1 and s2.
s1 = s2        // Copy s2 to s1.
s1 == s2       // Returns true if s1 and s2 are the same; otherwise returns false.
s1 != s2       // Returns true if s1 and s2 are not the same; otherwise returns false.
s1 < s2        // using dictionary ordering to compare
s1 <= s2
s1 > s2
s1 >= s2
s + "aa"        // ok, returns a string
s + 'a'         // ok, returns a string
"aa" + "bb"     // wrong
s + "aa" + "bb" // ok
  • Important note: use string::size_type when using string:
string::size_type len = s.size(); // good
auto len = s.size();              // good
int = s.size();                   // not good
decltype(s.size()) index = 0;     // good

Dealing with the characters in a string

  • Suggestion: use #include <cname> instead of #include <name.h> in C++ programs. Names defined in <cname> are defined inside the std namespace, while those defined in the name.h files are not.
  • The range for statement:
for (declaration : expression)
{
    statements
}

Example:

string s("hello");
for (auto &c : s) // must be reference to make the change
{
    c = toupper(c);
}
for (auto c : s)
{
    cout << c << endl;
}

Library vector type

Defining and initializing vectors

Adding elements to a vector

Other vector operations