3.1 节练习
练习 3.1
#include <iostream>
using namespace std;
int main ( ) {
cout << "using namesapce std" << endl;
string inPut;
cin >> inPut;
cout << "Your input is: " << inPut << endl;
return 0 ;
}
3.2
3.2.1
3.2.2 节练习
#include <iostream>
#include <string>
using namespace std;
int main ( ) {
string input;
while ( getline ( cin, input) ) {
cout << "Your input is: " + input << endl;
if ( input == "break" ) {
break ;
}
}
while ( cin >> input) {
cout << "The word You input is: " + input << endl;
if ( input == "break" ) break ;
}
string a, b;
while ( cin >> a >> b) {
if ( a == b) {
cout << a + " is equal to " + b << endl;
} else
{
cout << a + " is not equal to " + b << endl;
if ( a. size ( ) > b. size ( ) ) {
cout << a + " is longer than " + b << endl;
} else {
cout << b + " is longer than " + a << endl;
}
}
if ( a == "break" || b == "break" ) break ;
}
string res;
while ( cin >> input) {
if ( res == "" ) res = input;
else res = res + " " + input;
}
cout << res << endl;
return 0 ;
}
3.2.3 节练习
#include <iostream>
#include <string>
using namespace std;
int main ( ) {
string s;
cin >> s;
for ( auto & c: s) {
c = 'X' ;
}
cout << s << endl;
cin >> s;
for ( auto c: s) {
c = 'X' ;
}
cout << s << endl;
cin >> s;
decltype ( s. size ( ) ) index = 0 ;
if ( ! s. empty ( ) ) {
while ( index < s. size ( ) ) {
s[ index] = 'X' ;
++ index;
}
}
cout << s << endl;
cin >> s;
for ( decltype ( s. size ( ) ) i = 0 ; i < s. size ( ) ; ++ i) {
s[ i] = 'X' ;
}
cout << s << endl;
cout << "remove punc" << endl;
cin >> s;
string res;
for ( auto c : s) {
if ( ! ispunct ( c) ) res + = c;
}
cout << res << endl;
const string tmp ( "qwe" ) ;
for ( auto & c : tmp) cout << c << endl;
return 0 ;
}
3.3
3.3.1 节练习
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main ( ) {
vector< vector< int >> ivec;
vector< string> s{ 10 , "hi" } ;
return 0 ;
}
3.3.2 节练习
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main ( ) {
string input;
vector< string> strs;
while ( cin >> input) {
if ( input == "break" ) break ;
strs. push_back ( input) ;
}
cout << strs. size ( ) << endl;
int val;
vector< int > vals;
while ( cin >> input) vals. push_back ( val) ;
cout << vals. size ( ) << endl;
return 0 ;
}
3.3.3 节练习
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main ( ) {
string input;
vector< string> strs;
while ( cin >> input) {
if ( input == "break" ) break ;
for ( auto & c : input) c = toupper ( c) ;
strs. push_back ( input) ;
}
for ( auto & s : strs) cout << s << endl;
vector< int > v1 ( 10 , 42 ) ;
vector< int > v2{ 42 , 42 , 42 , 42 , 42 , 42 , 42 , 42 , 42 , 42 } ;
vector< int > v3;
for ( decltype ( v3. size ( ) ) i = 0 ; i != 10 ; ++ i) {
v3. push_back ( 42 ) ;
}
for ( auto i: v1) cout << i;
cout << endl;
for ( auto i: v2) cout << i;
cout << endl;
for ( auto i: v3) cout << i;
cout << endl;
int val;
vector< int > vals;
while ( cin >> val) {
if ( val == - 1 ) break ;
vals. push_back ( val) ;
}
for ( decltype ( vals. size ( ) ) i = 0 ; i < vals. size ( ) ; i + = 2 ) {
if ( i + 1 < vals. size ( ) )
cout << vals[ i] + vals[ i+ 1 ] << endl;
else
cout << vals[ i] << endl;
}
vector< int > vals2;
while ( cin >> val) {
vals2. push_back ( val) ;
}
for ( decltype ( vals2. size ( ) ) i = 0 ; i < vals2. size ( ) ; ++ i) {
cout << vals2[ i] + vals2[ vals2. size ( ) - 1 - i] << endl;
}
return 0 ;
}
3.4
3.4.1 节练习
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main ( ) {
vector< int > v1, v2 ( 10 ) , v3 ( 10 , 42 ) , v4{ 10 } , v5{ 10 , 42 } ;
vector< string> v6 ( 10 ) , v7 ( 10 , "hi" ) ;
for ( auto it = v1. cbegin ( ) ; it != v1. cend ( ) ; ++ it) cout << * it;
cout << endl;
for ( auto it = v2. cbegin ( ) ; it != v2. cend ( ) ; ++ it) cout << * it;
cout << endl;
for ( auto it = v3. cbegin ( ) ; it != v3. cend ( ) ; ++ it) cout << * it;
cout << endl;
for ( auto it = v4. cbegin ( ) ; it != v4. cend ( ) ; ++ it) cout << * it;
cout << endl;
for ( auto it = v5. cbegin ( ) ; it != v5. cend ( ) ; ++ it) cout << * it;
cout << endl;
for ( auto it = v6. cbegin ( ) ; it != v6. cend ( ) ; ++ it) cout << * it;
cout << endl;
for ( auto it = v7. cbegin ( ) ; it != v7. cend ( ) ; ++ it) cout << * it;
cout << endl;
vector< string> text{ "qwe" , "" , "asd" , "" , "zxc" } ;
for ( auto it = text. cbegin ( ) ; it != text. cend ( ) && ! it- > empty ( ) ; ++ it)
cout << * it << endl;
vector< int > vals;
for ( int i = 0 ; i != 10 ; ++ i) vals. push_back ( i) ;
for ( auto it = vals. begin ( ) ; it != vals. end ( ) ; ++ it) * it = 2 * * it;
for ( auto val: vals) cout << val << " " ;
cout<< endl;
return 0 ;
}
3.4.2 节练习
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main ( ) {
vector< int > vals;
for ( int i= 0 ; i != 10 ; ++ i) vals. push_back ( i) ;
for ( auto begin = vals. begin ( ) , end = vals. end ( ) - 1 ; begin != vals. end ( ) ; ++ begin, -- end) {
cout << * begin + * end << " " ;
}
cout << endl;
for ( auto begin = vals. begin ( ) ; begin < vals. end ( ) ; begin + = 2 ) {
if ( ( begin + 1 ) < vals. end ( ) )
cout << * begin + * ( begin + 1 ) << " " ;
else
cout << * begin << " " ;
}
cout << endl;
vector< unsigned > scores ( 11 ) ;
unsigned input;
while ( cin >> input) {
if ( input <= 100 ) {
auto it = scores. begin ( ) + input / 10 ;
* it + = 1 ;
}
}
for ( auto i: scores) cout << i << " " ;
cout << endl;
vector< unsigned > scores ( 11 ) ;
auto b = scores. begin ( ) , e = scores. end ( ) ;
auto mid = b + ( e - b) / 2 ;
return 0 ;
}
3.5
3.5.1 节练习
#include <iostream>
#include <string>
using namespace std;
string sa[ 10 ] ;
int ia[ 10 ] ;
int main ( ) {
const unsigned bf = 10 ;
int ary1[ bf] ;
int ary2[ 7 * 7 - 9 ] ;
char a[ 10 ] = "123456789" ;
string sa2[ 10 ] ;
int ia2[ 10 ] ;
for ( auto i: sa) cout << i << " " ;
cout << endl;
for ( auto i: ia) cout << i << " " ;
cout << endl;
for ( auto i: sa2) cout << i << " " ;
cout << endl;
for ( auto i: ia2) cout << i << " " ;
cout << endl;
return 0 ;
}
3.5.2 节练习
#include <iostream>
#include <vector>
using namespace std;
int main ( ) {
const size_t sz = 10 ;
int array[ sz] ;
for ( size_t i = 0 ; i < sz; ++ i) array[ i] = i;
for ( auto c: array) cout << c << " " ;
cout << endl;
int array_cp[ sz] ;
for ( size_t i = 0 ; i < sz; ++ i) array_cp[ i] = array[ i] ;
for ( auto c: array_cp) cout << c << " " ;
cout << endl;
vector< int > vals;
for ( decltype ( vals. size ( ) ) i = 0 ; i < 10 ; ++ i) vals. push_back ( i) ;
for ( auto c: vals) cout << c << " " ;
cout << endl;
return 0 ;
}
3.5.3 节练习
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main ( ) {
int a[ 10 ] ;
int * p1 = a;
int * p2 = end ( a) ;
p1 + = p2 - p1;
p2 + = p1 - p2;
for ( auto c: a) cout << c << " " ;
cout << endl;
p1 = begin ( a) , p2 = end ( a) ;
for ( p1; p1 != p2; ++ p1) * p1 = 0 ;
for ( auto c: a) cout << c << " " ;
cout << endl;
vector< int > iv1 ( 10 , 2 ) , iv2 ( 10 , 3 ) ;
if ( iv1 > iv2) cout << "iv1 > iv2" << endl;
if ( iv1 < iv2) cout << "iv1 < iv2" << endl;
else cout << "iv1 = iv2" << endl;
int a1[ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 } , a2[ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 } ;
auto sz1 = end ( a1) - begin ( a1) , sz2 = end ( a2) - begin ( a2) ;
if ( sz2 != sz1) cout << "a1 != a2" << endl;
else {
int * pa1 = a1, * pa2 = a2;
while ( pa1 != end ( a1) && pa2 != end ( a2) ) {
if ( * pa1 != * pa2) {
cout << "a1 != a2" << endl;
break ;
}
++ pa1, ++ pa2;
}
if ( pa1 == end ( a1) && pa2 == end ( a2) ) cout << "a1 = a2" << endl;
}
return 0 ;
}
3.5.4 节练习
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int main ( ) {
const char ca[ ] = { 'h' , 'e' , 'l' , 'l' , 'o' , '\0' } ;
const char * p = ca;
while ( * p) {
cout << * p << " " ;
++ p;
}
cout << endl;
string s1 = "hello" , s2 = "hell" ;
cout << ( s1 > s2) << endl;
const char c1[ ] = { 'h' , 'e' , 'l' , 'l' , 'o' , '\0' } , c2[ ] = { 'h' , 'e' , 'l' , 'l' , '\0' } ;
cout << ( strcmp ( c1, c2) ) << endl;
char c3[ 42 ] ;
strcpy ( c3, c1) ;
strcat ( c3, " " ) ;
strcat ( c3, c2) ;
for ( char * p = c3; * p; ++ p) cout << * p;
cout << endl;
return 0 ;
}
3.5.5 节练习
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main ( ) {
int array[ ] = { 1 , 2 , 3 , 4 , 5 } ;
vector< int > vi ( begin ( array) , end ( array) ) ;
for ( auto i : vi) cout << i << " " ;
cout << endl;
vector< int > vals ( 10 , 2 ) ;
int arr[ 10 ] ;
int * p = arr;
for ( decltype ( vals. size ( ) ) i = 0 ; i < vals. size ( ) ; ++ i) {
cout << vals[ i] << " " ;
* p = vals[ i] ;
++ p;
}
cout << endl;
for ( p = begin ( arr) ; p != end ( arr) ; ++ p) cout << * p << " " ;
cout << endl;
return 0 ;
}
3.6 节练习
#include <iostream>
using namespace std;
int main ( ) {
constexpr int row = 3 , col = 4 ;
int mat[ row] [ col] = { 0 } ;
for ( int ( & i) [ col] : mat) {
for ( int j : i) {
cout << j << " " ;
}
cout << endl;
}
cout << endl;
for ( size_t i = 0 ; i < row; ++ i) {
for ( size_t j = 0 ; j < col; ++ j) {
cout << mat[ i] [ j] << " " ;
}
cout << endl;
}
cout << endl;
for ( int ( * p) [ col] = mat; p != mat + row; ++ p) {
for ( int * q = * p; q != * p + col; ++ q) {
cout << * q << " " ;
}
cout << endl;
}
cout << endl;
using int_array = int [ 4 ] ;
for ( int_array * p = mat; p != mat + row; ++ p) {
for ( int * q = * p; q != * p + 4 ; ++ q) {
cout << * q << " " ;
}
cout << endl;
}
cout << endl;
for ( auto & p: mat) {
for ( auto q: p) {
cout << q << " " ;
}
cout << endl;
}
cout << endl;
return 0 ;
}