記事のディレクトリ
第3章文字列、ベクトルと配列
主なコンテンツ | 機能 |
---|---|
ストリング | 可変長文字列 |
ベクター | 可変長コレクション |
配列 | 同様のベクトル |
イテレータ | 同様のポインタ |
名前空間のステートメントの3.1を使用して
使用せずに[!]ヘッダファイル
using namespace std;//全部
using std::cin;//某个
3.2標準ライブラリの文字列型
#include<string>
using std::string;
3.2.1文字列を定義し、初期化します
string s1 ;
string s2 (s1);
string s2 =s1;
string s3 ("value") ;
string s3 = "value" ;
string s4(n,'c');
オブジェクト上の3.2.2文字列操作
os<<s;输出流如std::cout
is>>s;输入流如std::cin
getline (is, s);
s.empty ();
s.size ();
s[n];
s1+s2;
s1=s2;
s1==s2;
s1!=s2;
<, <=, >, >=;
string s;
while(cin>>s){}//非法输入结束
//保留空白字符
while(getline(cin,s)){
if(!s.empty()){..}
if(!s.size()>1){..}
}
//size()函数返回string::size_type类型的值,无符号数
オペレーティング | 説明 |
---|---|
サイズ()関数 | 戻り値string :: size_type型の符号なし数 |
+ | スプライシングおよび文字列は、両方のは、少なくとも文字列オブジェクトを有することができます |
3.2.3プロセス文字列オブジェクト
#include<cctype>
//cname的头文件,属于std空间
レンジ用
string str="aaa";
for(auto c:str){
cout<<c<<endl;
}//c为char
文字を変更します
for (auto &c : s)
{c = toupper(c);}
cout << s << endl;
添字演算子
输入参数:string::size_type|整数即可
返回:引用
string::size_type类型获取方法:decltype(s.size())
//例子
string s="dwa";
for(int i=0;i!=s.size();++i){
cout<<s[i]<<endl;
}
3.3標準ライブラリ型ベクター
#include<vector>
using std::vector//是一个类模板
vector<int> v1;
vector<Sales_item> v2;//<...>内不可以是引用,因为引用不是对象
vector<vector<int>> v3;
3.3.1定義と初期化ベクトルオブジェクト
サポートリストは、デフォルトの初期化、コピーによって初期化...
3.3.2ベクトルオブジェクトに要素を追加します
v.push_back(i);//添加到末尾
[!]範囲のために、{...}文は内部添加元素を持たないことに注意してください
3.3.3その他のベクトル演算
オペレーティング | 説明 |
---|---|
v.size() | ベクトル<...> :: size_type型を返します |
v.empty() | |
V [n]は | |
= | |
==、!=、<、>、<=、> = |
レンジ用
//改变
for(auto &i:v){
...
}
[!]のベクトル対空気は添字を持つ要素を追加することはできません
3.4はじめにイテレータ
イテレータオブジェクトをサポートしています |
---|
ストリング |
標準ライブラリのコンテナ |
3.4.1イテレータを使用して
イテレータメンバー | 説明 |
---|---|
ベギン() | 最初の要素の反復子を返します。 |
終わり() | 最後の要素の次の位置を返します。 |
コンテナは空で、その後、開始=最後は同じイテレータです
3.4.2イテレータ操作
オペレータ(ITERのイテレータ) | 説明 |
---|---|
*プロセス | 逆参照演算子が、戻り要素は[参照]と呼ばれるものの |
iter->メンバー||(* ITER)。メンバー | メンバーを取得逆参照 |
++、 - | モバイルユニット |
== ,! =、+、 - 、比較 | タイプは、一般的に、自動車を使用しています |
(* ITER).empty() | |
iter1-iter2 | 戻り距離、正または負のいずれか、difference_type |
[!]逆参照イテレータはのための法的でなければなりません
参照型を取得します。
string s("adada");
if(s.begin()!=s.end()){
auto iter =s.begin();
*iter=toupper(*iter);//*iter获取该元素的引用,可以对其修改成大写
}
循環のための!=、ライブラリコンテナイテレータのほとんどではないので、<演算子
for(auto it=s.begin();it!=s.end();++it){
...
}
イテレータ型
イテレータ型 | 機能(読み書き) |
---|---|
ベクトル::イテレータIT1。 | R、W |
文字列::イテレータIT2。 | R、W |
ベクトル:: const_iteratorのIT3。 | R |
文字列:: const_iteratorのIT4。 | R |
[!]定数イテレータはconst_iteratorのでなければなりません
[!]、beginとコンテナの終わりが依存することができるの戻り値はconstのイテレータ、constのイテレータ定数を返します)(イテレータ定数を返したい場合は、C ++ 11本の紹介はcbegin、逆を返し、CEND()
[!]イテレータのループは、要素が容器に追加することはできません
3.5アレイ
サイズが固定されています。
内蔵された配列の3.5.1の定義と初期化
外形寸法は定数式でなければなりません
constexpr unsigned sz=10;
int *p[sz];//p是数组,元素是int指针
[!]デフォルト値で初期化配列を定義する機能は、未定義含まれています
文字の配列
char a1[]={'a','\0'};//长度是2
char a2[]="c";//长度是2,隐含一个'\0'
割り当てをコピーすることはできません
//int a[]={0,1,2};
//int a2[]=a;//error
//a2=a;//error
ポインタの配列
int arr[]={1,2,3};
int *p=arr;//p指向arr
cout<<*p<<endl;//1
//也可以写成
int (*p2)[3]=&arr;//p2指向arr
cout<<*(*p2)<<endl;//1
cout<<*p2<<endl;//0x61fe34
cout<<p2<<endl;//0x61fe34
配列への参照
int arr[]={1,2,3};
int (&arr2)[3]=arr;
cout<<arr2[1]<<endl;//2
ポインタ要素の配列があります。
int *p[10];//p的元素是指针
int *(&p2)[10]=p;//p2是p的引用
3.5.2アクセス・アレイ素子
添字タイプ |
---|
size_tの |
レンジ用
for(auto i:array){}
3.5.3ポインタと配列
配列名は、最初の要素のアドレスでなく、ポインタであります
int a[]={1,2,3};
auto a2(a);//a2是指向a的指针
decltype(a) a3={2,3,4};//但是decltype(a) 返回int[3]
ポインタもイテレータ
int a[]={1,2,3};
int *p=a;
++p;
cout<<*p<<endl;//2
int *end=&a[3];//尾后迭代器
--end;
cout<<*end<<endl;//3
標準ライブラリ関数は、終わりを開始します
int a[]={1,2,3};
int *be=begin(a);
int *en=end(a);
cout<<*be<<" "<<*(en-1)<<endl;//1 3
添字とポインタ
int a[]={1,2,3,4,5,6};
int *p=&a[2];//*p是3
int j=p[1];//相当于*(p+1),4
int k=p[-2];//可正可负,1
3.5.4 Cスタイルの文字列
#include<cstring>
コピー、スプライスC
char s1[]="";
char s2[]="wda";
strcpy(s1,s2);//copy
strcat(s1,"ss");//拼接
cout<<s1<<endl;//wdass
古いコードとのインタフェース3.5.5
Cスタイルの文字列と文字列(文字の「\ 0」末端[])
string s3=s1;//wdass,可以赋值
s3+=s2;//"wdasswda",可以+,但是不能+两边都是char[]
[!]は、文字列の文字ポインタで初期化することはできません。
文字の文字列変数の配列
//char *p=s3;//error
const char *p=s3.c_str();//p指向s3变成的char[]
配列の初期化ベクトル
int a[]={1,2,3,44};
vector<int> v1(begin(a)+1,end(a));//2 3 44
3.6多次元配列
#ifdef CONSTEXPR_VARS
constexpr size_t rowCnt = 3, colCnt = 4;
#else
const size_t rowCnt = 3, colCnt = 4;
#endif
int ia[rowCnt][colCnt];
for (size_t i = 0; i != rowCnt; ++i) {
for (size_t j = 0; j != colCnt; ++j) {
...
}
}
//范围for,外层要用auto &
for (const auto &row : ia)
for (auto col : row)
cout << col << endl;
多次元配列とポインタ
int a[2][2]={{1,2},{3,4}};
int (&a2)[2]=a[1];//a2[2]={3,4}
int (*p)[2]=a;//*p={0,1}
トラバーサル
#ifdef CONSTEXPR_VARS
constexpr size_t rowCnt = 3, colCnt = 4;
#else
const size_t rowCnt = 3, colCnt = 4;
#endif
int ia[rowCnt][colCnt];
for (auto p = ia; p != ia + rowCnt; ++p) {
// q points to the first element of an array of four ints;
// that is, q points to an int
for (auto q = *p; q != *p + colCnt; ++q)
...
}
for (auto p = begin(ia); p != end(ia); ++p) {
// q points to the first element in an inner array
for (auto q = begin(*p); q != end(*p); ++q)
...
}
タイプの別名
#ifdef TYPE_ALIAS_DECLS
using int_array = int[4]; // new style type alias declaration
#else
typedef int int_array[4]; // equivalent typedef declaration
#endif
//int_array *p=ia跟int (*p)[4]=ia一样
for (int_array *p = ia; p != ia + 3; ++p) {
for (int *q = *p; q != *p + 4; ++q)
cout << *q << ' ';
cout << endl;
}