文章目录
- STL容器 vector
- 1. 管理学生信息
- 1.1 输入并输出
- 1.2 文件的存入与读出
- 1.3 文件的上传和下载
- 1.4 对信息增、删、改、查
- 2. 遍历数组
- 2.1 使用容器和迭代器
- 2.2 修改为迭代器遍历
- 2.3 关键词 auto
- 2.4 基于范围的for循环
STL容器 vector
向量容器(vector):从后面快速插入与删除,可以直接访问任何元素
1. 管理学生信息
1.1 输入并输出
使用 vector 容器输入多位学生信息,并输出
#include <iostream>
#include <vector>
using namespace std;
class Student{
string name;
int age;
float score;
public:
Student():name(),age(0),score(0.0){} // 构造函数
Student(const string& name,int age,float score):name(name),age(age),score(score){}
friend ostream& operator << (ostream& os,const Student& s){ // 输出流运算符重载
return os << s.name << "\t" << s.age << "\t" << s.score;
}
friend istream& operator >> (istream& is,Student& s){ // 输入流运算符重载
return is >> s.name >> s.age >> s.score;
}
};
int main(){
vector<Student> vec;
while(true){
Student s;
cin >> s;
if(!cin.good()) break; // 不是好的状态即结束输入
vec.push_back(s); // 尾插入
}
for(int i=0;i<vec.size();++i){
cout << vec[i] << endl;
}
}
结果为:
张三 23 98.2
李四 22 92.3
王五 21 92.2
张三 23 98.2
李四 22 92.3
王五 21 92.2
1.2 文件的存入与读出
从终端写入文件,再从文件中读出并写入文件
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Student{
string name;
int age;
float score;
public:
Student():name(),age(0),score(0.0){}
Student(const string& name,int age,float score):name(name),age(age),score(score){}
friend ostream& operator << (ostream& os,const Student& s){
return os << s.name << "\t" << s.age << "\t" << s.score;
}
friend istream& operator >> (istream& is,Student& s){
return is >> s.name >> s.age >> s.score;
}
};
int main(){
vector<Student> vec;
/* // 从终端读
while(true){
Student s;
cin >> s;
if(!cin.good()) break; // 不是好的状态即结束输入
vec.push_back(s); // 尾插入
}
*/
/* // 从文件中读
ifstream fin("./student",ios::in);
while(true){
Student s;
fin >> s;
if(!fin.good()) break;
vec.push_back(s);
}
fin.close();
*/
// 从文件读(简)
ifstream fin("./student",ios::in);
Student s;
while(fin >> s){
vec.push_back(s);
}
fin.close();
// 往文件里面写
ofstream fout("./student",ios::out);
for(int i=0;i<vec.size();++i){
cout << vec[i] << endl;
fout << vec[i] << endl;
}
fout.close();
}
结果为:
[root@foundation1 C++6.19]# ./a.out
张三 23 98.2
李四 22 92.3
王五 21 92.5
[root@foundation1 C++6.19]# cat student
张三 23 98.2
李四 22 92.3
王五 21 92.5
1.3 文件的上传和下载
对文件上传信息,从文件下载信息
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Student{
string name;
int age;
float score;
public:
Student():name(),age(0),score(0.0){}
Student(const string& name,int age,float score):name(name),age(age),score(score){}
friend ostream& operator << (ostream& os,const Student& s){
return os << s.name << "\t" << s.age << "\t" << s.score;
}
friend istream& operator >> (istream& is,Student& s){
return is >> s.name >> s.age >> s.score;
}
};
class StudentsInfo{
vector<Student> vec;
public:
void Load(const string& path){
// 从文件读
ifstream fin(path,ios::in);
Student s;
while(fin >> s){
vec.push_back(s);
}
fin.close();
}
void Save(const string& path){
// 往文件里面写
ofstream fout(path,ios::out);
for(int i=0;i<vec.size();++i){
// cout << vec[i] << endl;
fout << vec[i] << endl;
}
fout.close();
}
friend ostream& operator << (ostream& os,const StudentsInfo& si){
os << "姓名\t年龄\t成绩" << endl;
for(int i=0;i<si.vec.size();++i){
os << si.vec[i] << endl;
}
return os;
}
};
int main(){
StudentsInfo info;
info.Load("./student"); // 下载学生信息
cout << info << endl;
}
结果为:
姓名 年龄 成绩
张三 23 98.2
李四 22 92.3
王五 21 92.5
1.4 对信息增、删、改、查
对学生信息进行增、删、改、查
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Student{
string name;
int age;
float score;
public:
Student():name(),age(0),score(0.0){}
Student(const string& name,int age,float score):name(name),age(age),score(score){}
string GetName()const{ // 返回一个名字
return name;
}
friend ostream& operator << (ostream& os,const Student& s){
return os << s.name << "\t" << s.age << "\t" << s.score;
}
friend istream& operator >> (istream& is,Student& s){
return is >> s.name >> s.age >> s.score;
}
};
class StudentsInfo{
vector<Student> vec;
public:
void Load(const string& path){
ifstream fin(path,ios::in);
Student s;
while(fin >> s){
vec.push_back(s);
}
fin.close();
}
void Save(const string& path){
ofstream fout(path,ios::out);
for(int i=0;i<vec.size();++i){
// cout << vec[i] << endl;
fout << vec[i] << endl;
}
fout.close();
}
friend ostream& operator << (ostream& os,const StudentsInfo& si){
os << "姓名\t年龄\t成绩" << endl;
for(int i=0;i<si.vec.size();++i){
os << si.vec[i] << endl;
}
return os;
}
// 添加学生信息
void Append(const Student& s){
vec.push_back(s); // 底部添加一个元素
}
// 查找学生信息
Student Search(const string& name)const{
for(int i=0;i<vec.size();++i){
if(vec[i].GetName() == name){
return vec[i];
}
}
return Student();
}
// 修改学生信息
bool Update(const Student& s){
for(int i=0;i<vec.size();++i){
if(vec[i].GetName() == s.GetName()){
vec[i] = s;
return true;
}
}
return false;
}
// 删除学生信息
bool Delete(const string& name){
bool found = false;
for(int i=0;i<vec.size();++i){
if(vec[i].GetName() == name){
found = true;
}else{
if(found){ // 找到名字后的每一个都往前移
vec[i-1] = vec[i];
}
}
}
if(found) {
vec.resize(vec.size()-1);
return true;
}
return false;
}
};
int main(){
StudentsInfo info;
info.Load("./student");
const char* items[] = {
"查看学生信息",
"添加学生信息",
"查找学生信息",
"修改学生信息",
"删除学生信息"
};
for(int i=0;i<5;++i){
cout << (i+1) << "." << items[i] << endl;
}
cout << "清选择操作:";
int n;
cin >> n;
switch(n){
case 1:
cout << info << endl;
break;
case 2:{
Student s;
cin >> s;
info.Append(s);
info.Save("./student");
break;
}
case 3:{
cout << "请输入姓名:";
string name;
cin >> name;
Student s = info.Search(name);
if(s.GetName().empty()){ // 如果名字是空
cout << "没有找到学生" << name << endl;
}else{
cout << s << endl;
}
break;
}
case 4:{
cout << "请输入姓名、年龄、成绩:";
Student s;
cin >> s;
if(info.Update(s)){
cout << s.GetName() << "更新成功" << endl;
info.Save("./student");
}else{
cout << s.GetName() << "更新失败" << endl;
}
break;
}
case 5:
cout << "请输入姓名:";
string name;
cin >> name;
if(info.Delete(name)){
cout << name << "删除成功" << endl;
info.Save("./student");
}else{
cout << name << "删除失败" << endl;
}
break;
}
}
结果为:
1.查看学生信息
2.添加学生信息
3.查找学生信息
4.修改学生信息
5.删除学生信息
清选择操作:5
请输入姓名:赵六
赵六删除成功
2. 遍历数组
2.1 使用容器和迭代器
对一组数组遍历打印,多种做法
#include <iostream>
#include <vector>
using namespace std;
int main(){
// 数组下标方式遍历
int arr[] = {1,3,5,7,8};
for(int i=0;i<5;++i){
cout << arr[i] << " ";
}
cout << endl;
// 字符串下标方式遍历
const char* s = "abcde";
for(int i=0;'\0'!=s[i];++i){
cout << s[i] << " ";
}
cout << endl;
// 字符串指针方式遍历
const char* p = s;
while('\0' != *p){
cout << *p++ << " ";
}
cout << endl;
// 数组指针方式遍历
int* q = arr;
while(q != arr+5){ // 是最后一个有数据地址的下一个,表示数组结束
cout << *q++ << " ";
}
cout << endl;
// 容器;向量(与下标遍历类似)
vector<int> vec = {1,4,6,8,9};
for(int i=0;i<vec.size();++i){ // 向量下标方式遍历
cout << vec[i] << " ";
}
cout << endl;
// 迭代器遍历(与指针遍历类似)
vector<int>::iterator it = vec.begin(); // 获取第一个元素的地址
while(it != vec.end()){ // 获取最后一个元素的下一个地址,表示数组结束
cout << *it << " ";
++it;
}
cout << endl;
}
结果为:
1 3 5 7 8
a b c d e
a b c d e
1 3 5 7 8
1 4 6 8 9
1 4 6 8 9
其中 *it 的值是可以修改的
如果不修改 *it 的值,尽量修改为:
// 迭代器遍历
vector<int>::const_iterator cit = vec.cbegin();
while(cit != vec.cend()){
cout << *cit << " ";
++cit;
}
cout << endl;
2.2 修改为迭代器遍历
把上述例子,学生信息管理程序改一下,都改成迭代器
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Student{
string name;
int age;
float score;
public:
Student():name(),age(0),score(0.0){}
Student(const string& name,int age,float score):name(name),age(age),score(score){}
string GetName()const{ // 返回一个名字
return name;
}
friend ostream& operator << (ostream& os,const Student& s){
return os << s.name << "\t" << s.age << "\t" << s.score;
}
friend istream& operator >> (istream& is,Student& s){
return is >> s.name >> s.age >> s.score;
}
};
class StudentsInfo{
vector<Student> vec;
public:
void Load(const string& path){
ifstream fin(path,ios::in);
Student s;
while(fin >> s){
vec.push_back(s);
}
fin.close();
}
void Save(const string& path){
ofstream fout(path,ios::out);
vector<Student>::iterator it = vec.begin(); // 使用迭代器
while(it != vec.end()){
fout << *it++ << endl;
}
fout.close();
}
friend ostream& operator << (ostream& os,const StudentsInfo& si){
os << "姓名\t年龄\t成绩" << endl;
vector<Student>::const_iterator it = si.vec.cbegin(); // 使用迭代器
while(it != si.vec.cend()){
os << *it++ << endl;
}
return os;
}
void Append(const Student& s){
vec.push_back(s);
}
Student Search(const string& name)const{
vector<Student>::const_iterator it = vec.cbegin(); // 使用迭代器
while(it != vec.cend()){
if(it->GetName() == name) return *it;
// if((*it).GetName() == name) return *it;
}
return Student();
}
bool Update(const Student& s){
vector<Student>::iterator it = vec.begin(); // 使用迭代器
while(it != vec.end()){
if(it->GetName() == s.GetName()) {
*it++ = s;
return true;
}
}
return false;
}
bool Delete(const string& name){
bool found = false;
vector<Student>::iterator it = vec.begin(); // 使用迭代器
while(it != vec.end()){
if(it->GetName() == name){
found = true;
}else{
if(found){
*(it-1) = *it;
}
}
++it;
}
if(found) {
vec.resize(vec.size()-1);
return true;
}
return false;
}
};
int main(){
StudentsInfo info;
info.Load("./student");
const char* items[] = {
"查看学生信息",
"添加学生信息",
"查找学生信息",
"修改学生信息",
"删除学生信息"
};
for(int i=0;i<5;++i){
cout << (i+1) << "." << items[i] << endl;
}
cout << "清选择操作:";
int n;
cin >> n;
switch(n){
case 1:
cout << info << endl;
break;
case 2:{
Student s;
cin >> s;
info.Append(s);
info.Save("./student");
break;
}
case 3:{
cout << "请输入姓名:";
string name;
cin >> name;
Student s = info.Search(name);
if(s.GetName().empty()){ // 如果名字是空
cout << "没有找到学生" << name << endl;
}else{
cout << s << endl;
}
break;
}
case 4:{
cout << "请输入姓名、年龄、成绩:";
Student s;
cin >> s;
if(info.Update(s)){
cout << s.GetName() << "更新成功" << endl;
info.Save("./student");
}else{
cout << s.GetName() << "更新失败" << endl;
}
break;
}
case 5:
cout << "请输入姓名:";
string name;
cin >> name;
if(info.Delete(name)){
cout << name << "删除成功" << endl;
info.Save("./student");
}else{
cout << name << "删除失败" << endl;
}
break;
}
}
2.3 关键词 auto
C++11 中的 auto关键词
自动推导类型,简化迭代器的写法
// 迭代器遍历(与指针遍历类似)
/* vector<int>::iterator it = vec.begin(); */
auto it = vec.begin(); // 获取第一个元素的地址
while(it != vec.end()){ // 获取最后一个元素的下一个地址,表示数组结束
cout << *it << " ";
++it;
}
cout << endl;
或者:
// 迭代器遍历(与指针遍历类似)
for(auto it = vec.begin();it != vec.end();++it){
cout << *it << " ";
++it;
}
cout << endl;
2.4 基于范围的for循环
C++11 基于范围的for循环
int arr[] = {1,3,5,7,8};
for(auto n:arr){ // for(int n:arr){
cout << n << " ";
}
cout << endl;
类似于
for(int i=0,n=arr[0];n=arr[i],i<5;++i)
或者:
vector<int> vec = {1,4,6,8,9};
for(auto n:vec){ // for(int n:vec){
cout << n << " ";
}
cout << endl;
或者:
string str = "abcde";
for(auto c:str){ // for(char c:str){
cout << c << " ";
}
cout << endl;