c++標準ライブラリタイプstring使用全解(参考書C++primer第5版)

33563 ワード

stringオブジェクトの初期化
#include 
using namespace::std;
int main()
{
	string s1;//     ,s1      
	string s2 = s1;//s2 s1   
	string s2(s1);//     
	string s3 = "hiya";//s3           //     
	string s3("hiya");//     //     
	string s4(10, 'c');//s4    cccccccccc//     

}

stringオブジェクト上の操作
#include 
#include 
using namespace::std;
int main()
{
	//os<
	//is>>s  is        s,        ,  is
	string s;
	cin >> s;
	cout << s << endl;
	//           
	string s1, s2;
	cin >> s1 >> s2;
	cout << s1 << s2 << endl;
	//       string  
	string word;
	while (cin >> word)//    ,        
		cout << word << endl;
	//  getline     ,             ,                    string  
	//getline(is, s)  is       s,  is
	string line;
	while (getline(cin, line))
	//      ,      80     
		if(line.size() > 80)
			cout << line << endl;
	//s.empty() s    true,    false
	//s.size()   s      ,      string::size_type      string     ,       
	//  ,    auto decltype   
	

	
	
	//  string  
	//    string             , string                      
	string slang1 = "hello";
	string slang2 = "hiya";
	if (slang1 < slang2);//true
	//    string            string            string            ,     string       string  
	string str1 = "hey";
	string str2 = "heyyyy";
	if (str1 < str2);//true

	// string    
	//s1 = s2  s2     s1      
	string st1(10, 'c'), st2;
	st1 = st2;//  st1 st2      

	//  string    
	//s1 + s2   s1 s2      
	string s1 = "hello, ", s2 = "world.
"
; string s3 = s1 + s2;//hello, world.
s1 += s2;//hello, world.
// string // , string string s4 = "hello " + "," + s2;// , string temp = "hello " + ","; string s4 = s2 + temp // C ,string //s[n] s n , n 0 return 0; }

練習する
#include 
#include 
using namespace::std;
//  3.2
//            
void a() {
	string str;
	while(1)
		getline(cin, str);
}
//                
void b() {
	string str;
	while (1)
		cin >> str;
}

//  3.4
//       ,      ,     ,        
void c() {
	string str1, str2;
	if (str1 == str2)
		cout << " str1 equal to str2" << endl;
	else {
		cout << "no equal" << endl;
		if (str1 > str2)
			cout << str1 << endl;
		else
			cout << str2 << endl;
	}
}
//             
void d() {
	string str1, str2;
	if (str1.size() == str2.size())
		cout << "str1 str2  " << endl;
	else {
		if (str1.size() > str2.size())
			cout << str1 << endl;
		else
			cout << str2 << endl;
	}
}

//  3.5
//                       
void e() {
	string word, phrase;
	while (cin >> word) {
		phrase += word;
	}
	cout << phrase << endl;
}
//    ,               
void f() {
	string word, phrase;
	while (cin >> word) {
		cout << word << " " << endl;
	}
}

stringオブジェクトの文字の処理
#include 
#include 
#include 
#include 
using namespace::std;
int main()
{
	//cctype       
	//isalnum(c)  c         
	//isalpha(c)  c      
	//iscntrl(c)  c       
	//isdigit(c)  c      
	//isgraph(c)  c            
	//islower(c)  c        
	//isupper(c)  c        
	//isprint(c)  c         ( c          )
	//ispunct(c)  c        
	//isspace(c)  c      (  、     、     、   、   、   )
	//isxdigit(c) c          
	//tolower(c)   c     ,         ;      c
	//toupper(c)   c     ,         ;      c

	//       for         C++11
	//for (declaration : expression)	statement
	//expression         
	//declaration        ,            
	//    ,declaration           expression         


	string str("some string");
	//    str      
	for (auto c : str) {
		Sleep(200);
		cout << c << endl;
	}
	cout << endl;
	
	
	//  string          
	string s("Hello ECNU Software Engineering Institute");
	for (auto c : s) {
		Sleep(200);
		cout << c << endl;
	}
	auto upper_cnt = 0;
	for (auto c : s)
		if (isupper(c))
			upper_cnt++;
	cout << upper_cnt << endl;

	//    for           ,             
	//              
	for (auto &c : s) 
		c = toupper(c);//c     ,    s    
	cout << s << endl;

	//        
	//     []      string::size_type    ,              ,             
	//s[s.size()-1]       

	//              
	string s2("hi ecnu ");
	if (!s2.empty())
		s[0] = toupper(s[0]);

	//        , ecnu    
	for (decltype(s2.size()) index = 3;
		index != s2.size() && !isspace(s[index]); ++index)
		s[index] = toupper(s[index]);

	//          
	// 0 15                   
	const string hexdigits = "0123456789ABCDEF";
	cout << "      :";
	string result;//            
	string::size_type n;//             
	while (cin >> n)
		if (n < hexdigits.size())//       0-15  
			result += hexdigits[n] + " ";
	cout << "         :" << result << endl;


}

練習する
#include
#include
#include
using namespace::std;
int main()
{
	//  3.6    for             X  
	string s1("Tomorrow is 520");
	for (auto &c : s1)
		c = 'X';
	cout << s1 << endl;

	//  3.7 3.6          char     
	for (char &c : s1)
		c = 'O';
	cout << s1 << endl;
	//  ,auto &c  char &c

	auto i = 0;
	//  3.8   while   for    3.6
	for (i = 0; i < s1.size(); i++)
		s1[i] = 'X';
	i = 0;
	while (i < s1.size())
	{
		s1[i] = 'X';
		i++;
	}

	//3.9          ,    ?
	string s;
	if(!s.empty())//     
		cout << s[0] << endl;
	//     string  s      ,   string    ,     

	//3.10              ,                 
	string ss1, ss2;
	getline(cin, ss1);
	for (auto c : ss1)
	{
		if (!ispunct(c))
			ss2 += c;
	}
	cout << ss2 << endl;

	//3.11     for     ,    ,c      
	const string sss = "Keep out!";
	for (auto &c : s) {
		//  ,         ,   c  sss   
	}
	return 0;
}