c++prime习题集阅读笔记一

命名空间

在我们的C/C++语言中,变量、函数以及类都是大量存在的,这些变量、函数以及类都 是存在于全局域中的,一不小心,我们就有可能有冲突,再比如,假如我们以后写项目,要链接第三方的库文件,那么是不是就很有可能我们自己起的名字就有可能和别人的冲突,在我们的C语言中,我们只能靠将名字尽量起的复杂一点来解决这个问题,但是也还是无法完全避免,所以为了避免这种冲突,C++就引入了命名冲突这种机制,来避免命名冲突,解决命名冲突的问题。

#include <iostream>
using namespace std;
namespace text {
//声明一个命名空间,注意要有大括号隔开
	int a = 10;
	namespace text1
//嵌套定义一个声明命名空间
	{
		int b = 20;
	}
}
void main() {
	using namespace text;
	using namespace text::text1;//访问引用text1内的命名空间的元素
	cout << a << endl;
	cout << b << endl;
}

总的来说,命名空间的存在意义就是为了区分重定义,在不同的命名空间里,相同的名字单代表的不是一个元素。这就好比如双胞胎兄弟,分别在不同的班级,然后这样就可以很好地区分他们的区别。要是他们都在一个班级,这就难搞哦。

Using的用法

using 的用法,它可以利用已经封装好的命名空间的函数,无需声明namespace。
而上面的例子就是利用namespace声明空间存储数据元素。

#include<iostream>
#include<string>

using std::cout;
using std::cin;
using std::endl;
//用using做声明

int main()
{
	int a,b;
	while(cin>>a>>b)
	{
		if(a>b)
		cout<<a<<b<<endl;
		else
		cout<<b<<" "<<a<<endl;
	}
	return 0;
}

Geline和cin在输入字符串时的区别

1、	cin输入一个单词,遇到空白处自动结束
2、	getline 输入整个字符串,遇到空白处不结束,当遇到换行时自动结束。
#include<iostream>
#include<string>

using namespace std;

int main()
{
	string word,line;
	cout<<"请选择你的输入方式:\n1、单独一个单词输入\n2、把整个一串输入"<<endl;
	char ch;
	cin>>ch;
	if(ch == '1')
	{
		cout<<"请输入你的字符串:"<<endl;
		cin>>word;
		cout<<"字符串的有效长度为:"<<endl;
		cout<<word<<endl;;
	}
	cin.clear();
	cin.sync();
	//清空缓存区域 
	if(ch == '2')
	{
		cout<<"请输入你的字符串:"<<endl;
		getline(cin,line); 
		cout<<"字符串的有效长度为:"<<endl;
		cout<<line<<endl;
	}
	return 0;
}

String类

String 类定义的数据类型可以进行加法运算,就是把各个字符串连接起来。

#include<iostream>
#include<string>

using namespace std;

int main()
{
	char str='y';
	string line,result;
	cout<<"请输入你的字符串数据:"<<endl;
	while(cin>>line)
	{
		if(!result.size())
		result+=line;
		else
		result=result+" "+line;
		cout<<"请选择你是否要继续输入字符串?(y/n)"<<endl;
		cin>>str;
		if(str=='y'||str=='Y')
		continue;
		else
		break;
	}
	cout<<"最终的字符串数据为:"<<endl;
	cout<<result<<endl;
	return 0;
}

vector类

vector是一个类模板,模板本身不是类和函数,相反可以将模板看作是编译器生成类或函数编写的一份声明。
vector能够容纳大多数类型的对象作为其元素,但是因为引用不是对象,所以不存在包含引用的vector。除此之外,其他大多数内置类型和类类型都可以构成vector对象。

1. vector对象初始化

(1).vector<T>v1   //v1是一个空vector,它潜在的元素是T类型的,执行默认初始化。

(2).vector<T>v2(v1) 或vector<T> v2 = v1  //v2包含v1中所有元素的副本

(3).vector<T>v3(n, val)  //v3中包含n个重复的元素,每个元素的值都是val

(4).vector<T>v4(n)  //v4包含n个重复的执行了初始化的对象

(5).vector<T>v5{a, b, c……} 或 vector<T> v5 = {a, b, c……}  //v5包含了初始值个数的元素

2. vector对象操作

(1).vec.push_back()  //在vector对象尾部添加元素

(2).vec.empty()  //判定vector对象是否为空

(3).vec.size()  //返回vec对象的元素个数

(4).vec[n-1]  //引用vector对象的第n个对象

(5).vec.pop_back()   //删除vector对象尾部最后一个元素
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>

class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

bool myCompare(Person &v1, Person &v2) {
	return v1.m_Age > v2.m_Age;
}

void test01() {
	vector<Person> v;

	Person p1("刘备", 24);
	Person p2("关羽", 28);
	Person p3("张飞", 25);
	Person p4("赵云", 21);
	Person p5("诸葛", 33);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	sort(v.begin(), v.end(), myCompare);

	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
		cout << "姓名:" << it->m_Name << ",年龄:" << it->m_Age << endl;
	}
	
	cout << endl;
}

int main() {
	test01();
	system("pause");
	return 0;
}
#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int sum = 10;
	vector<int>Vint;
	int str[sum];
	for(int i=0;i<sum;i++)
	Vint.push_back(i);
	for(int j=0;j<sum;j++)
	cout<<Vint[j]<<" ";
	cout<<endl;
	cout<<Vint.size()<<endl;;
	Vint.clear();
	for(int j=0;j<sum;j++)
	cout<<Vint[j]<<" ";
	cout<<endl;
	
	return 0;
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值