Advertisement
Malinovsky239

std::string

Oct 12th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string> // library for string data type
  4.  
  5. using namespace std;
  6. // necessary in order to work with any C++library
  7. // (in that case, with <string> and <iostream>)
  8.  
  9. int main() {
  10.     string s = "";
  11.     /* creates empty string.
  12.     One can initialize the string with any value.
  13.     E.g.:
  14.     string s = "abacaba";
  15.     */
  16.  
  17.     for (char i = 'a'; i <= 'z'; i++)
  18.         s += i;
  19.     /* Adds symbols in alphabitical order.
  20.  
  21.     Attention!
  22.  
  23.     += works with complexity in proportion
  24.     to the size of the part we add
  25.  
  26.     + (s = s + i) works in proportion
  27.     to the total size of the string we obtain
  28.     after addition
  29.  
  30.     So,
  31.     for (int i = 0; i < int(1e5); i++)
  32.         s = s + 'a';
  33.     leads to TL,
  34.  
  35.     but
  36.     for (int i = 0; i < int(1e5); i++)
  37.         s += 'a';
  38.     doesn't.
  39.  
  40.     */
  41.  
  42.     cout << s.size() << endl; // outputs string length
  43.     cout << s.length() << endl; // the same as the previous one command
  44.  
  45.     int cnt_less_n = 0;
  46.     for (int i = 0; i < s.size(); i++) {
  47.         // shows how one can get i-th symbol of the string (0 <= i < s.size())
  48.         if (s[i] < 'n') // comparison of two symbols (by their codes)
  49.             cnt_less_n++;                  
  50.     }
  51.     cout << cnt_less_n << " symbols less than n in " << s << endl;
  52.  
  53.     // first parameter = index of the leading symbol in the substring
  54.     // second parameter = length of substring
  55.     cout << s.substr(3, 5) << endl;
  56.  
  57.     // if one need to get some suffix of the string,
  58.     // he is permitted to omit the second parameter
  59.     cout << s.substr(3) << endl;
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement