1.Introduction to the C++ Language

  • 2020 年 2 月 18 日
  • 笔记

1.Storing Vectors

#include <iostream>  #include <vector>  using std::cout;  using std::vector;      int main(){        //1d      vector<int> v1{0,1,2};      vector<int> v2 = {3,4,5};      vector<int> v3;      v3 = {6,7,8};      cout << "1d initial ok"<<"n";      cout << v1[2] << "n";      cout << v2[0] << "n";      cout << v3[1] << "n";          //2d      vector<vector<int>> v2d {{1,2},{7,8}};      cout << "2d initial ok"<<"n";      cout << v2d[1][1] << "n";    }

2. Comments

// You can use two forward slashes for single line comments.    /*  For longer comments, you can enclose the text with an opening  slash-star and closing star-slash.  */ 

3.use Auto

在之前的代码中,每个变量的类型都已明确声明。通常,这不是必需的,编译器可以根据分配的值确定类型。要自动确定类型,请使用auto关键字。

#include <iostream>  #include <vector>  using std::vector;  using std::cout;    int main() {      auto i = 5;      auto v_6 = {1, 2, 3,7,8,9};      cout << "Variables declared and initialized without explicitly stating type!" << "n";        for (auto i :v_6){          cout << i << " ";      }      cout << "n";  }

4 working with vector

#include <iostream>  #include <vector>  using std::vector;  using std::cout;    int main() {        //////////////////////      //1D Vector Access      /////////////////////      vector<int> a = {0, 1, 2, 3, 4};      cout << a[0];      cout << a[1];      cout << a[2];      cout << "n";      //Getting a Vector's Length      cout << a.size()<<"n";        ////////////////////////      //2D Vector Access      ///////////////////////      vector<vector<int>> b = {{1, 1, 2, 3},                               {2, 1, 2, 3},                               {3, 1, 2, 3}};        cout << b[2][1];      cout << "n";      //Getting a Vector's Length      cout << b.size()<<"x"<<b[0].size()<<"n";    }

5 Reading from a File

#include <iostream>  #include <string>  //add this for file  #include <fstream>  void reading_data_from_stream(){      std::fstream my_file("files/1.board");      if(my_file){          std::cout << "The file stream has been created!" << "n";          std::string line;          while (getline(my_file, line)){              std::cout << line << "n";          }      }  }

Four steps to reading a file: 1.#include <fstream> 2.Create a std::ifstream object using the path to your file. 3.Evaluate the std::ifstream object as a bool to ensure that the stream creation did not fail. 4.Use a while loop with getline to write file lines to a string.

String

#include <iostream>  #include <string>  #include <sstream>    using std::cout;  using std::string;  using std::istringstream;      /*  myreader is a pointer, whith point to the contain of string.  if the current pointer READ(>>) the non-number or error or endoffile  the myreader will return error or 0 or false  every time you extract a contain, the myrerader will move right to next contain.  */      void istringstream_test() {      cout << __func__ << "n";        string a("1 2 3");        istringstream my_stream(a);        int n;      my_stream >> n;      cout << n << "n";  }      void use_isstringstream_as_boolen_read_all() {      cout << __func__ << "n";        string a("1 2 3");        istringstream my_stream(a);        int n;        // Testing to see if the stream was successful and printing results.      while (my_stream) {          my_stream >> n;          if (my_stream) {              cout << "That stream was successful: " << n << "n";          }          else {              cout << "That stream was NOT successful!" << "n";          }      }  }    void common_way_to_use_istringstream_in_while() {      cout << __func__ << "n";        istringstream myreader("1 2 3");      int n;      while (myreader >> n) {          cout << "read: " << n << "n";      }      cout << "The stream has failed or ended." << "n";  }        void string_with_MIX_types_not_space() {      /*      In the stream example above, the string contained only whitespaces      and characters which could be converted to ints.      If the string has mixed types, more care is needed to process the string.      In the following example,      the type char is used, which is a type that can hold only a single ASCII character.      */      cout << __func__ << "n";        string b("1,2,3,4,6q7p8o9");        istringstream mixstring(b);        //need two type of tmp value      char c;      int n;        /*          !! notice that the 9 was not printed          mixstring >> n >> c          tried to stream an int followed by a char.          Since there was no char after the 9, the stream          failed and the while loop exited.      */      while (mixstring >> n >> c) {          cout << "read int: " << n << ", read char: " << c << "n";      }      cout << "The stream has failed or ended." << "n";  }      int main() {      //stream with all INT type      istringstream_test();      use_isstringstream_as_boolen_read_all();      common_way_to_use_istringstream_in_while();        //stream with MIX type      //the INT spreated by only one char not space      string_with_MIX_types_not_space();  }

6. ParseLine

vector<int> ParseLine_row(string line) {      istringstream sline(line);      int n;      char c;      vector<int> row;      while (sline >> n >> c && c == ',') {        row.push_back(n);      }      return row;  }

7.Enum

#include <iostream>  using std::cout;    int main()  {      // Create the enum Color with fixed values.      enum class Color { white, black, blue, red };        // Create a Color variable and set it to Color::blue.      Color my_color = Color::blue;        // Test to see if my car is red.      if (my_color == Color::red) {          cout << "The color of my car is red!" << "n";      }      else {          cout << "The color of my car is not red." << "n";      }  }
enum class State {kEmpty, kObstacle};    vector<State> ParseLine(string line) {      istringstream sline(line);      int n;      char c;      vector<State> row;      while (sline >> n >> c && c == ',') {        if (n == 0) {          row.push_back(State::kEmpty);        } else {          row.push_back(State::kObstacle);        }      }      return row;  }