c++primerノート----文字列、ベクトル、配列
14083 ワード
using
声明#include
using std::cin;
using std::cout; using std::endl;
string
string s1; // ,s1
string s2(s1);
string s2 = s1;
string s3 = "value";
string s3("value");
string s4(n, 'c');
string
string s;
cin >> s; // ( 、 、 ),
getline(cin. line); // , 。
line.empty();
auto len = line.size(); //len `string::size_type`。 unsigned
// size() , int 。
// int-->unsigned int
stirng
加算:文字列のフォント値はstringオブジェクトstring s4 =s1 +", ";
string s5 = "hello " + ", "; // : + string
for
サイクル#include
using std::string;
string s("hello");
decltype(s.size()) punct_cnt = 0; //
for(auto c : s) // auto &c : s 。c char
if(ispunct(c)) //
++punct_cnt;
[]
:受信パラメータタイプはstring::size_type
です.シンボルタイプなし.c++
標準ライブラリは、下標の合法性vevtor
:クラステンプレート、コンパイラ生成クラスのために作成された説明#include
using std::vector
vector<int> ivec; // <> ,
vector
vector v1; // vector
vector v2(v1);
vector v2 = v1;
vector v3(n, val);
vector v4(n); //n ,
vector v5{a, b, c};
vector v5 = {a, b, c};
vector<string> v6("hi"); //error : {}
vector<string> v7{10}; // v7(10).
vector<string> v8{10, "hi"}; // v8(10, "hi")
vector
操作vector<int> v1;
v1.push_back(1); // 。 , for
auto b = v.begin(), e = v.end(); // ,
for(auto it = s.begin(); it != s.end(); ++it)
//!= , ++it it++
vector<int>::iterator it;
string::iterator it2;
vector<int>::const_iterator it3;
string::const_iterator it4;
vector<int> v;
const vector<int> cv;
auto it1 = v.begin(); //vector::iterator ,
auto it2 = cv.begin(); //vector::const_iterator
auto it3 = v.cbegin(); //vector::const_iterator
it->mem;
(*it).mem;
constexpr
と定数式:値は変更されず、コンパイルプロセスで計算結果の式が得られます.字面値は定数式に属し、定数式で初期化されたconstオブジェクトも定数式const int max_files = 20;
const int limit = max_files + 1;
int staff_size = 27; //
const int sz = get_size(); // constexpr
constexpr int mf =20;
int arr[10]; //[]
int a2[] = {0, 1, 2}; // 3
int a3[5] = {0, 1, 2}; // 2 0
char a1[] = {'c', 'd'}; // 2
char a2[] = {'c', 'd', '\0';} //
char a3[] = "c++"; // 4,
const char a4[6] = "daniel"; //error: 7,
int *ptrs[10]; //ptrs 10 。
int &refs[10]; //error : 。
int (*parray)[10] = &arr; //parray , 10
int (&arrref)[10] = arr; //arrref , 10
int *(&arry)[10] = ptrs; //arry , 10 int
num++
が不正であり、num++
は配列の先頭を指す定数であり、左値としてはならない.string nums[] = {"1", "2", "3"};
string *p = &nums[0]; //[]
string
オブジェクトをCスタイル文字列char *str = s.c_str(); // 。
// ,
vector
オブジェクトint int_arr[] = {0, 1, 2, 3, 4, 5};
vector<int> ivec(begin(int_arr, end(int_arr))); //, int*
int ia[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};//3 , 4
int ia[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
int ia[3][4] = {{1}, {2}, {3}};
int (&row)[4] = ia[1]; // row ia 4
for
size_t cnt = 0; // [] size_t
for(auto &row:ia) // auto row:ia。 int[4] ,
// 。 &,
for(auto &col:row){ // , &
col = cnt;
}
int ia[3][4];
int (*p)[4] = ia; // -->
p = &ia[2];
int (&row)[4] = ia[1]; // row ia 4
for(auto p=ia; p!=ia+3; ++p) // --> , p , 4
for(auto q=*p; q!=*p+4; ++q) //*p ,*P , -->
for(auto p=begin(ia); p!=end(ia);++p)
for(auto q=begin(*p); q!=end(*p); ++q)
using int_array = int[4];
typedef int int_array[4];