Experiment 2 array, pointer and C + + standard library

4, Experimental conclusion

1. Experimental tasks 1-4

Summary:

  • Correlation and difference of ordinary array, array and vector

    1. Memory allocation method: both array and array are static memory allocation and are located in the stack. vector uses dynamic memory allocation and is located in the heap.

    2. Efficiency: array = array > vector

    3. Copy: both vector and array can copy one object directly to another. Arrays can only be copied element by element.

  • Iterators and pointer dependencies, and differences

    relevance:

    1. Both pointer and iterator support +, - operations with integers, and their meaning is to move n positions forward or backward from the current position.

    2. Both pointer and iterator support subtraction. Pointer pointer gets the distance between two pointers, and iterator iterator gets two pointers
      The distance between iterators.

    3. You can modify the element it points to through a pointer or an iterator.

    difference:

    1. The cout operator can directly output the value of the pointer, but an error will be reported when operating on the iterator. Through reading the error message and header file, we know that the iterator returns the object reference rather than the object value, so cout can only output the value obtained by the iterator using * instead of directly outputting itself.

    2. A pointer can point to a function but not an iterator. An iterator can only point to a container

      This shows that iterators and pointers are actually completely different concepts. Pointer is a special variable, which is specially used to store the address of another variable, and iterator is only an STL interface designed with reference to the characteristics of pointer.

  • Difference between C style string and string

    The biggest difference is that the C-style string is static and cannot be changed dynamically. It is very troublesome to use. The dynamic management of std::string type in C + + is very convenient.

    C-style strings are different from char arrays. See the following two definitions:
    char carr1 = {'a', 'b', 'c'};
    char carr2 = {'a', 'b', 'c', '\0'};
    From the above, carr2 can be said to be a C-style string, carr1 is not a C-style string, and the C-style string must end with '\ 0'.
    String class is a class of the standard library, not a built-in type. The standard library is like a class defined by ourselves. String type objects do not end with '\ 0' as standard. If you need to use the string class to assign a value to a C-style string, you need to add '\ 0' after it.

2. Experimental task 5

  • Info.hpp file source code

    #ifndef info_hpp
    #define info_hpp
    #include <iostream>
    using namespace std;
    
    class info
    {
    public:
      info(string x, string y, string z, int w) : nickname{x}, contact{y}, city{z}, n{w} {};
      ~info() = default;
      void print() const;
    
    private:
      string nickname, contact, city;
      int n;
    };
    
    void info::print() const
    {
      cout << "Address:     " << nickname << "\n";
      cout << "contact information:    " << contact << "\n";
      cout << "City:    " << city << "\n";
      cout << "Scheduled number:    " << n << "\n";
      return;
    }
    #endif
    
  • task5.cpp source code

    #include "info.hpp"
    #include <iostream>
    #include <vector>
    using namespace std;
    
    const int capacity = 100;
    
    int main()
    {
      vector<info> audience_info_list;
      string x, y, z;
      int w, sum = 0;
      char op;
      cout << "Input information:\n\n";
      cout << "call/nickname"
    ​     << "Contact information (email)/Mobile phone number),"
    ​     << "City,"
    ​     << "Scheduled participants" << endl;
      while (cin >> x >> y >> z >> w)
      {
    ​    if (sum + w > capacity)
    ​    {
    ​      cout << "Sorry, there's only one left" << capacity - sum << "Two positions." << endl;
    ​      cout << "1. input u,Renew( update)Reservation information" << endl;
    ​      cout << "2. input q,Exit reservation" << endl;
    ​      cout << "Your choice:";
    ​      cin >> op;
    ​      if (op == 'q')
    ​        break;
    ​      else
    ​        continue;
    ​    }
    ​    info a(x, y, z, w);
    ​    sum += w;
    ​    audience_info_list.push_back(a);
    ​    if (sum == capacity)
    ​      break;
      }
      cout << endl;
      cout << "So far, there are" << sum << "Listeners are scheduled to attend. The information of the scheduled audience is as follows:" << endl;
      for (auto i : audience_info_list)
      {
    ​    i.print();
      }
      return 0;
    }
    
  • Screenshot of running test results

3. Experimental task 6

  • TextCoder.hpp file source code

    #ifndef textcoder_hpp
    #define textcoder_hpp
    #include <iostream>
    using namespace std;
    
    class TextCoder
    {
    public:
      TextCoder(string x) : text{x} {};
      ~TextCoder() = default;
      string encoder();
      string decoder();
    
    private:
      string text;
    };
    
    string TextCoder::encoder()
    {
      for (int i = 0; i < text.length(); i++)
      {
    ​    if (text[i] >= 'a' && text[i] <= 'z')
    ​    {
    ​      text[i] = 'a' + (text[i] - 'a' + 5) % 26;
    ​    }
    ​    if (text[i] >= 'A' && text[i] <= 'Z')
    ​    {
    ​      text[i] = 'A' + (text[i] - 'A' + 5) % 26;
    ​    }
      }
      return text;
    }
    
    string TextCoder::decoder()
    {
      for (int i = 0; i < text.length(); i++)
      {
    ​    if (text[i] >= 'a' && text[i] <= 'z')
    ​    {
    ​      text[i] = 'a' + (text[i] - 'a' + 21) % 26;
    ​    }
    ​    if (text[i] >= 'A' && text[i] <= 'Z')
    ​    {
    ​      text[i] = 'A' + (text[i] - 'A' + 21) % 26;
    ​    }
      }
      return text;
    }
    #endif
    
  • task6.cpp source code

    #include "textcoder.hpp"
    #include <iostream>
    #include <string>
    
    int main()
    {
      using namespace std;
    
      string text, encoded_text, decoded_text;
    
      cout << "Enter English text: ";
      while (getline(cin, text))
      {
    ​    encoded_text = TextCoder(text).encoder();  // A temporary nameless object is used here
    ​    cout << "Encrypted English text:\t" << encoded_text << endl;
    
    ​    decoded_text = TextCoder(encoded_text).decoder(); // A temporary nameless object is used here
    ​    cout << "Decrypted English text:\t" << decoded_text << endl;
    ​    cout << "\n Enter English text: ";
      }
    }
    
  • Screenshot of running test results

5, Experimental summary (optional)

Posted by border20 on Mon, 01 Nov 2021 23:37:48 -0700