C++ Primer 学习笔记_6_标准库类型 -- 命名空间using与string类型

本文介绍了C++标准库中的命名空间使用方法与string类型的基本操作,包括string的定义、初始化、比较、读取、赋值等,并通过示例展示了如何利用string处理文本数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



标准库类型(一)

--命名空间usingstring类型



引:

     标准库类型是语言组成部分中更主要的哪些数据类型(如:数组、指针)的抽象!

C++标准库定义的是高级的抽象数据类型:

   1、高级:由于当中反映了更复杂的概念。

   2、抽象:由于我们在使用时不须要关心他们是怎样表示的,我们仅仅须要知道这些抽象数据类型支持哪些操作就能够了。


正文:

一、命名空间的using声明

1 using std::cin;

    ::运算符的作用含义是右操作数的名字能够在左操作数的作用域中找到。

格式:

  1. using namespace::name;  
  2. //一旦使用了using声明,我们就能够直接引用名字,而不须要再引用该名字的命名空间!

      
using namespace::name;
//一旦使用了using声明,我们就能够直接引用名字,而不须要再引用该名字的命名空间!


   演示样例:

  1. #include <iostream>  
  2. using std::cin;  
  3. using std::cout;  
  4. using std::endl;  
  5. int main()  
  6. {  
  7.     cout << "Enter two numbers:" << endl;  
  8.   
  9.     int v1, v2;  
  10.     cin >> v1 >> v2;  
  11.   
  12.     cout << "The sum of " << v1   
  13.          << " and " << v2  
  14.          << " is " << v1 + v2 << endl;  
  15.   
  16.     return 0;  
  17. }  
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
	cout << "Enter two numbers:" << endl;

	int v1, v2;
	cin >> v1 >> v2;

	cout << "The sum of " << v1 
	     << " and " << v2
	     << " is " << v1 + v2 << endl;

	return 0;
}

2、在一种情况下。必须总是使用全然限定的标准库名字:在头文件里。

    通常,头文件里应该仅仅定义确实必要的东西。请养成这个好习惯!



二、标准库string类型

string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存。以及提供各种实用的操作

  1. #include <string>  
  2. using std::string;  
#include <string>
using std::string;

1string对象的定义和初始化

  1. //四种定义及初始化方式  
  2.     string s1;  
  3.     string s2(s1);  
  4.     string s3("value");  
  5.     string s4(n,'c');  
//四种定义及初始化方式
	string s1;
	string s2(s1);
	string s3("value");
	string s4(n,'c');

2string与字符串字面值的异同

1)都是以'\0'结尾

  1. string s1("value");  
  2. for (string::size_type i = 0;s1[i] != '\0'; ++i)  
  3.     cout << s1[i] << ' ';  
  4. cout << endl;  
	string s1("value");
	for (string::size_type i = 0;s1[i] != '\0'; ++i)
		cout << s1[i] << ' ';
	cout << endl;


 2)字符串字面值与string根本就不是一个类型!

  1. string SI = "ABC";  
  2. char SII[] = "CDE";  
  3. string tmp = SI;  
  4. SI = string(SII);  
  5. strcpy(SII,tmp.c_str());  
  6. cout << SI << endl;  
  7. cout << SII << endl;  
	string SI = "ABC";
	char SII[] = "CDE";
	string tmp = SI;
	SI = string(SII);
	strcpy(SII,tmp.c_str());
	cout << SI << endl;
	cout << SII << endl;

3getline读取整行文本

    1)、getline函数从输入流的下一行读取。并保存读取内容到string中,可是不包含换行符

    2)、getline函数将stream參数作为返回值

    3)、因为getline在返回时丢弃换行符,所以换行符将不会保存在string对象中!


  1. //P72 习题3.5  
  2. int main()  
  3. {  
  4.     string line;  
  5.     while (getline(cin,line))  
  6.     {  
  7.         cout << line << endl;  
  8.     }  
  9.   
  10.     return 0;  
  11. }  
//P72 习题3.5
int main()
{
	string line;
	while (getline(cin,line))
	{
		cout << line << endl;
	}

	return 0;
}

  1. //3.5(2)  
  2. int main()  
  3. {  
  4.     string word;  
  5.     while (cin >> word)  
  6.     {  
  7.         cout << word << endl;  
  8.     }  
  9.   
  10.     return 0;  
  11. }  
//3.5(2)
int main()
{
	string word;
	while (cin >> word)
	{
		cout << word << endl;
	}

	return 0;
}


4、string对象的操作 size与empty

  1. //例程1  
  2.     string st("The expense of spirit\n");  
  3.     cout << "The size of " << st << "is " << st.size()  
  4.          << " characters, including the newline" << endl;  
//例程1
    string st("The expense of spirit\n");
    cout << "The size of " << st << "is " << st.size()
         << " characters, including the newline" << endl;

  1. //例程2  
  2.     string se;  
  3.     if (se.empty())  
  4.     {  
  5.         cout << "The string is empty!" << endl;  
  6.     }  
  7.     else  
  8.     {  
  9.         cout << "The size of " << se << "is " << se.size()  
  10.              << " characters, including the newline" << endl;  
  11.     }  
//例程2
	string se;
    if (se.empty())
    {
        cout << "The string is empty!" << endl;
    }
    else
    {
        cout << "The size of " << se << "is " << se.size()
             << " characters, including the newline" << endl;
    }

5string::size_type类型

    string类类型和更多库类型都定义了一些配套类型,通过这些配套类型。库类型的使用就能与机器无关(machine-independent)

string::size_type类型可以保证足够大到可以存储随意string对象的长度。

  1. string str = "Hello World";  
  2. string::size_type length = str.size();  
    string str = "Hello World";
    string::size_type length = str.size();

6string关系操作符

    ==,!=,<,<=,>=,>

  1. string big = "big",small = "small";  
  2. if (big == small)   //big <= small,big >=  small,big != small,...  
  3. {  
  4.     //...  
  5. }  
  6. else  
  7. {  
  8.     //...  
  9. }  
	string big = "big",small = "small";
	if (big == small)	//big <= small,big >=  small,big != small,...
	{
		//...
	}
	else
	{
		//...
	}

7string对象的赋值

  1. string s1,s2 = “value”;  
  2. s1 = s2;  
string s1,s2 = “value”;
s1 = s2;

    赋值操作须要做一些操作:它必须把s1占用的相关内存释放掉。然后再分配给s1足够存放s2副本的内存空间,最后把s2中全部的字符都拷贝到新分配的内存中!


8、两个string对象相加

  1. string s1("hello, ");  
  2. string s2("world\n");  
  3. string s3 = s1 + s2;  
  4. cout << s3 << endl;  
  5. string s4 = s2 + s1;  
  6. cout << s4 << endl;  
  7. string s5 = s3 + "! " + "I`m a programmer!";  
  8. cout << s5 << endl;  
	string s1("hello, ");
	string s2("world\n");
	string s3 = s1 + s2;
	cout << s3 << endl;
	string s4 = s2 + s1;
	cout << s4 << endl;
	string s5 = s3 + "! " + "I`m a programmer!";
	cout << s5 << endl;

9string对象str下标的取值范围:0str.size()-1

  1. string str ("Some thing!");  
  2. for (string::size_type i = 0; i != str.size(); ++i)  
  3. {  
  4.     cout << str[i];  
  5. }  
  6. cout << endl;  
	string str ("Some thing!");
	for (string::size_type i = 0; i != str.size(); ++i)
	{
		cout << str[i];
	}
	cout << endl;

10string对象下标能够用作左值

  1. string str ("Some thing!");  
  2. for (string::size_type i = 0; i != str.size(); ++i)  
  3. {  
  4.     str[i] = '*';  
  5. }  
  6. cout << str << endl;  
	string str ("Some thing!");
	for (string::size_type i = 0; i != str.size(); ++i)
	{
		str[i] = '*';
	}
	cout << str << endl;

11string对象中的字符处理函数

   isalnum,isalpha,iscntrl,isdigit,isgraph,islower,isprint,ispunct,isspace,isupper,isxdigit,tolower,toupper...


  1. //P78 习题3.7  
  2. //(1)  
  3. int main()  
  4. {  
  5.     string s1,s2;  
  6.     cin >> s1 >> s2;  
  7.     if (s1 > s2)  
  8.     {  
  9.         cout << s1 << " is bigger!" << endl;  
  10.     }  
  11.     else if (s1 < s2)  
  12.     {  
  13.         cout << s2 << " is bigger!" << endl;  
  14.     }  
  15.     else  
  16.     {  
  17.         cout << s1 << " is equal to " << s2 << endl;  
  18.     }  
  19.     return 0;  
  20. }  
//P78 习题3.7
//(1)
int main()
{
    string s1,s2;
    cin >> s1 >> s2;
    if (s1 > s2)
    {
        cout << s1 << " is bigger!" << endl;
    }
    else if (s1 < s2)
    {
        cout << s2 << " is bigger!" << endl;
    }
    else
    {
        cout << s1 << " is equal to " << s2 << endl;
    }
    return 0;
}

  1. //(2)  
  2. int main()  
  3. {  
  4.     string s1,s2;  
  5.     cin >> s1 >> s2;  
  6.     if (s1.size() > s2.size())  
  7.     {  
  8.         cout << s1 << " is longer!" << endl;  
  9.     }  
  10.     else if (s1.size() < s2.size())  
  11.     {  
  12.         cout << s2 << " is longer!" << endl;  
  13.     }  
  14.     else if (s1.empty() && s2.empty())  
  15.     {  
  16.         cout << s1 << " and " << s2 << " is empty!" << endl;  
  17.     }  
  18.     else  
  19.     {  
  20.         cout << s1 << " is equal to " << s2 << endl;  
  21.     }  
  22.     return 0;  
  23. }  
//(2)
int main()
{
    string s1,s2;
    cin >> s1 >> s2;
    if (s1.size() > s2.size())
	{
		cout << s1 << " is longer!" << endl;
	}
	else if (s1.size() < s2.size())
	{
		cout << s2 << " is longer!" << endl;
	}
	else if (s1.empty() && s2.empty())
	{
		cout << s1 << " and " << s2 << " is empty!" << endl;
	}
	else
	{
		cout << s1 << " is equal to " << s2 << endl;
	}
    return 0;
}

  1. //习题 3.8  
  2. //(1)  
  3. int main()  
  4. {  
  5.     string sub,str;  
  6.     while (cin >> sub)  
  7.     {  
  8.         str += sub;  
  9.     }  
  10.     cout << str << endl;  
  11.     return 0;  
  12. }  
//习题 3.8
//(1)
int main()
{
    string sub,str;
    while (cin >> sub)
	{
		str += sub;
	}
	cout << str << endl;
    return 0;
}

  1. //(2)  
  2. int main()  
  3. {  
  4.     string sub,str;  
  5.     cin >> sub;  
  6.     str = sub;  
  7.     while (cin >> sub)  
  8.     {  
  9.         str += ' ';  
  10.         str += sub;  
  11.     }  
  12.     cout << str << endl;  
  13.     return 0;  
  14. }  
//(2)
int main()
{
    string sub,str;
    cin >> sub;
    str = sub;
    while (cin >> sub)
	{
		str += ' ';
		str += sub;
	}
	cout << str << endl;
    return 0;
}

  1. //习题 3.9 以下的程序是否合法。合法的话会输出什么值?  
  2. int main()  
  3. {  
  4.     string s;  
  5.     cout << s[0] << endl;  
  6.     s[1] = '*';  
  7.     cout << s[1] << endl;  
  8.     return 0;  
  9. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值