1. 程式人生 > >c++ 中string和vector中的pop_back push_back的練習

c++ 中string和vector中的pop_back push_back的練習

C++中的string和vector

Example 1:

輸入一行字元的集合,遇到換行符結束輸入,並且判斷這個字串物件中的大寫、小寫、空格、數字、以及其他符號個數。

Code:

#include<iostream>
#include<string>
// #include<cctype>
using namespace std;
int main()
{
//統計輸入的字串中大寫字母、小寫字母、空格、數字、以及其他符號的數量
string str;
int upper_n{0},num_n{0},lower_n{0},space_n{0},other_n{0};
cout<<
"請輸入你想統計的字元:(遇到換行符跳出)"<<endl;
getline(cin,str); //進行判斷  /*c++11標準提供一種語句:範圍for (range for statement)  格式如下:  for(declaration : expression) { statement;     }   定義一個變數,然後這個變數去遍歷一個物件,然後對每次遍歷的物件進行操作。  */ for(auto c:str) {  if(isdigit(c))  num_n++;  else if(islower(c))  lower_n++;  else
if(isupper(c))
 upper_n++;  else if(isspace(c))  graph_n++;  else  other_n++; }  cout<<"小寫字母個數:"<<lower_n<<endl      <<"大寫字母個數:"<<upper_n<<endl      <<"數 字 個 數:"<<num_n<<endl      <<"空 格 個 數:"<<space_n<<endl      <<
"其他字元個數:"<<other_n<<endl;
 return 0; }

Run:

[email protected]:~/c++/c++bin$ ./built_in_type 
請輸入你想統計的字元:(遇到換行符跳出)
ABCDEFGH  --  abcdefgh @@@%%%% 12345678
小寫字母個數:8
大寫字母個數:8
數 字 個 數:8
空 格 個 數:6
其他字元個數:9

以上結果讓我產生了新的疑問,就是我並沒有呼叫cctype這個庫,為什麼可以正常呼叫{isupper(),isspace(),islower()},然後查了度娘:

有些觀點是:可能在其它 我匯入的庫中,有呼叫cctype。

然後我查看了我的iostream文件:

#ifndef _GLIBCXX_IOSTREAM
#define _GLIBCXX_IOSTREAM 1

#pragma GCC system_header

#include <bits/c++config.h>
#include <ostream>
#include <istream>

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  /**
   *  @name Standard Stream Objects
   *
   *  The &lt;iostream&gt; header declares the eight <em>standard stream
   *  objects</em>.  For other declarations, see
   *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/io.html
   *  and the @link iosfwd I/O forward declarations @endlink
   *
   *  They are required by default to cooperate with the global C
   *  library's @c FILE streams, and to be available during program
   *  startup and termination. For more information, see the section of the
   *  manual linked to above.
  */
  //@{
  extern istream cin;/// Linked to standard input
  extern ostream cout;/// Linked to standard output
  extern ostream cerr;/// Linked to standard error (unbuffered)
  extern ostream clog;/// Linked to standard error (buffered)

#ifdef _GLIBCXX_USE_WCHAR_T
  extern wistream wcin;/// Linked to standard input
  extern wostream wcout;/// Linked to standard output
  extern wostream wcerr;/// Linked to standard error (unbuffered)
  extern wostream wclog;/// Linked to standard error (buffered)
#endif
  //@}

  // For construction of filebuffers for cout, cin, cerr, clog et. al.
  static ios_base::Init __ioinit;

_GLIBCXX_END_NAMESPACE_VERSION
} // namespace

#endif /* _GLIBCXX_IOSTREAM */

此時的我有點懵,然後我看到一個我不認識的標頭檔案bits/c++config.h,但很不幸運,我沒有在/usr/include/bits裡找到它,我在/usr/include/x86_64-linux-gnu/c++/5.4.0/bits$找到了。

這個問題我查了一段時間,沒有結果,總之的出一個結論,不管有沒有呼叫這個標頭檔案,他都能執行成功,但最好的情況下還是呼叫該有的標頭檔案,防止沒有其它標頭檔案未包含該標頭檔案。

也希望如有大佬看到我這個遺留的問題,請告知一二,謝謝!!!

Vector 模板:

定義和初始化vector

vector
vector<T> var_1 是一個空型別vector
vector<T> var_2(var_1) var_2中包含所有var_1中的元素
vector<T> var_2=var_1 var_2中包含所有var_1中的元素
vector<T> var_3(n,var) var_3中包含n個var
vector<T> var_4(n) var_4包含n個初始化值
vector<T> var_5{1,2,3,4,5.....} 初始var_5的個數
vector<T>var_5={1,2,3,4,5....} 等價上一個
vector 是一個動態陣列

其實看到上述表中,我對於vector<T>var_4(n,比較好奇,所以我就寫了如下程式碼,來檢視它到底是怎樣初始化的。

Example 2:

//部分程式碼
int n=10;
vector<int> age(n);
for(auto i : age)
{
  cout<<i<<endl;
}

Run:

[email protected]:~/c++/c++bin$ ./built_in_type 
0
0
0
0
0
0
0
0
0
0

我想如果運用vector<T> var_4(n)這樣的形式初始化的話,其中var_4中的吃初始個數是n,初始值由系統自動初始化值。

如何對於vector物件進行操作呢?

push_back() and pop_back()

Example 3:

//部分程式碼
vector<string> students_name;
string name;
cout<<"請輸入學生姓名:(quit推出)"<<endl;
while(true)
{

  cin>>name;
  if(name=="quit")
  break;
  students_name.push_back(name);

}
cout<<"你們班同學名單:"<<endl;
for(auto i : students_name)
{
  cout<<i<<endl;
}
students_name.pop_back();
cout<<"你們班刪除最後一個同學的名單:"<<endl;
for(auto i : students_name)
{
  cout<<i<<endl;
}

Run:

[email protected]:~/c++/c++bin$ ./built_in_type 
請輸入學生姓名:(quit推出)
bob 
peter
alice
quit
你們班同學名單:
bob
peter
alice
你們班刪除最後一個同學的名單:
bob
peter

這樣可以看出,vector是一個動態陣列,也可以說是一個容器

而push_back和pop_back 的作用分別就是:

相當於把一個數據放入一個按照順序放入一個盒子裡,這個盒子裡有很多個格子並且是有順序的。

這些資料中的每一部分按照我們給定的規則去放入格子裡。

而push_back()的作用,就是把單個數據壓在盒子最裡面的那個格子裡。

pop_back(),就是我們把資料都儲存好了,它去掉這個盒子裡的最外面的那個資料。

   一步一步慢慢學,不怕學的比別人慢,就怕什麼也沒學懂。

----至自己