c++中vector<string>可以存放什么数据
4个回答
展开全部
利用C++做信号处理方面的仿真,于是就涉及到了大量数据的存储。由于在读取数据的时候,并不知道数据的长度,这时候,vector就很好用了,因为vector容器哪余袜不用知道数组的长度。
编写程序读入一组string类型的数据,并将它们存储在vector中,接李激着,把该vector对象复制给一个字符指针数组。为vector中的每个元素创建一个新的字符数组,并把毁简该vector元素的数据复制到相应的字符数组中,最后把指向该数组的指针插入字符指针数组。
扩展资料
vector拷贝使用总结
1、利用拷贝赋值操作符(深复制)
vector<int> array{3,5,2,6,4};
vector<int> outArray;
outArray = array;
2、利用拷贝构造(深复制)
vector<int> array{3,5,2,6,4};
vector<int> outArray(array);
3、利用swap()函数(交换两个vector)
会清空原vector数组
vector<int> array{3,5,2,6,4};
vector<int> outArray;//设为空
outArray.swap(array);//清空array数组
展开全部
vector是c++标准库的一个容器,氏野尘如果学脊物过数据结构就知道有数组,线性表,链表之类各种东西,歼禅vector实际上就是数组。
string是c++标准库的字符串类型。
实例:
vector<string> what_string;
vector<string> hell_string;
vector<string> louzhu_string;
vector<string> shafa_string;
string是c++标准库的字符串类型。
实例:
vector<string> what_string;
vector<string> hell_string;
vector<string> louzhu_string;
vector<string> shafa_string;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这这升宴种定义 可以存放 string类型的 字符并笑简串 . vector<int>这绝裤样声明可以存整型。 还可以定义为自定义类型
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
class Student
{
public:
Student(){}
void write();
void red();
void show();
void sortScore();
int operator<(const Student&);
int a_bScore();
void deleteStudent();
private:
int id;
string name;
double score;
};
vector<Student> student;
void Student::write()
{
Student s;
char fileName[20];
cout<<"橡慧请输入文件名:"<<endl;
cin>>fileName;
ofstream out(fileName,ios::app);
cout<<"请输入学生学号姓名成绩:"<<endl;
vector<Student>::iterator iter;
while (cin>>s.id>>s.name>>s.score)
{
iter=student.begin();
while (iter!=student.end())
{
if (s.id==iter->id)
{
cout<<s.id<<"已经存在。"<<endl;
cout<<iter->id<<" "<<iter->name<<" "<<iter->score<<endl;
break;
}
else
iter++;
}
if (iter==student.end())
{
out<<s.id<<" "<<s.name<<" "<<s.score<<" ";
student.push_back(s);
}
}
cin.clear();
}
void Student::show()
{
vector<Student>::const_iterator iter=student.begin();
cout<<"学号\t姓名\t 成绩\t"<<endl<<endl;
while (iter!=student.end())
{
cout<<iter->id<<"\t"<<iter->name<<" \t"<<iter->score<<endl;
iter++;
}
}
void Student::red()
{
Student s;
char fileName[20];
cout<<"请输入文件名:"<<endl;
cin>>fileName;
ifstream in(fileName);
vector<Student>::iterator iter;
while (in>>s.id>>s.name>>s.score)
{
iter=student.begin();
while (iter!=student.end())
{
if (s.id==iter->id)
{
break;
}
else
iter++;
}
if (iter==student.end())
{
student.push_back(s);
}
}
cin.clear();
}
int Student::operator<(const Student&s)
{
return score>s.score?1:0;
}
void Student::sortScore()
{
sort(student.begin(),student.end());
}
int Student::a_bScore()
{
int a,b,low,up,cnt=0;
cout<<碧丛"请输入要查找的分数段:"<<endl;
cin>>a>>b;
if (a>b)
{
up=a;
low=b;
}
else
{
low=a;
up=b;
}
vector<Student>::const_iterator iter=student.begin();
while (iter!=student.end())
{
if (iter->score>梁慧答=low&&iter->score<=up)
{
cout<<iter->id<<" "<<iter->name<<" "<<iter->score<<endl;
cnt++;
}
iter++;
}
return cnt;
}
void Student::deleteStudent()
{
Student s;
cout<<"请输入要删除的学生学号:"<<endl;
cin>>s.id;
vector<Student>::iterator iter=student.begin();
while (iter!=student.end())
{
if (iter->id==s.id)
{
break;
}
else
iter++;
if (iter!=student.end())
{
student.erase(iter);
cout<<"OK,"<<s.id<<"信息删除完毕。"<<endl;
}
else
{
cout<<"没找到学号为:"<<s.id<<"的学生信息,删除失败。"<<endl;
}
}
}
int main()
{
Student s;
int choice;
cout<<"=-=-=-=-=-=-=-=-=-=-=-=-=-=-欢迎使用学生信息管理系统-=-=-=-=-=-=-=-=-=-=-=-=-=-="<<endl;
begin:
cout<<"1->添加学生信息 2->显示学生信息 3->删除学生信息 "<<endl<<endl;
cout<<" 0-> 退出 "<<endl<<endl;
cout<<"4->从文件添加 5->按成绩排序 6->分数段信息查询"<<endl<<endl;
cin>>choice;
if (choice==1)
{
s.write();
system("cls");
goto begin;
}
else if (choice==2)
{
s.show();
system("pause");
system("cls");
goto begin;
}
else if (choice==3)
{
s.deleteStudent();
system("pause");
system("cls");
goto begin;
}
else if (choice==4)
{
s.red();
system("pause");
system("cls");
goto begin;
}
else if (choice==5)
{
s.sortScore();
system("pause");
system("cls");
goto begin;
}
else if (choice==6)
{
s.a_bScore();
system("pause");
system("cls");
goto begin;
}
else if (choice==0)
{
exit(0);
}
else
{
cout<<"输入非法!!!"<<endl;
system("pause");
goto begin;
}
return 0;
}
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
class Student
{
public:
Student(){}
void write();
void red();
void show();
void sortScore();
int operator<(const Student&);
int a_bScore();
void deleteStudent();
private:
int id;
string name;
double score;
};
vector<Student> student;
void Student::write()
{
Student s;
char fileName[20];
cout<<"橡慧请输入文件名:"<<endl;
cin>>fileName;
ofstream out(fileName,ios::app);
cout<<"请输入学生学号姓名成绩:"<<endl;
vector<Student>::iterator iter;
while (cin>>s.id>>s.name>>s.score)
{
iter=student.begin();
while (iter!=student.end())
{
if (s.id==iter->id)
{
cout<<s.id<<"已经存在。"<<endl;
cout<<iter->id<<" "<<iter->name<<" "<<iter->score<<endl;
break;
}
else
iter++;
}
if (iter==student.end())
{
out<<s.id<<" "<<s.name<<" "<<s.score<<" ";
student.push_back(s);
}
}
cin.clear();
}
void Student::show()
{
vector<Student>::const_iterator iter=student.begin();
cout<<"学号\t姓名\t 成绩\t"<<endl<<endl;
while (iter!=student.end())
{
cout<<iter->id<<"\t"<<iter->name<<" \t"<<iter->score<<endl;
iter++;
}
}
void Student::red()
{
Student s;
char fileName[20];
cout<<"请输入文件名:"<<endl;
cin>>fileName;
ifstream in(fileName);
vector<Student>::iterator iter;
while (in>>s.id>>s.name>>s.score)
{
iter=student.begin();
while (iter!=student.end())
{
if (s.id==iter->id)
{
break;
}
else
iter++;
}
if (iter==student.end())
{
student.push_back(s);
}
}
cin.clear();
}
int Student::operator<(const Student&s)
{
return score>s.score?1:0;
}
void Student::sortScore()
{
sort(student.begin(),student.end());
}
int Student::a_bScore()
{
int a,b,low,up,cnt=0;
cout<<碧丛"请输入要查找的分数段:"<<endl;
cin>>a>>b;
if (a>b)
{
up=a;
low=b;
}
else
{
low=a;
up=b;
}
vector<Student>::const_iterator iter=student.begin();
while (iter!=student.end())
{
if (iter->score>梁慧答=low&&iter->score<=up)
{
cout<<iter->id<<" "<<iter->name<<" "<<iter->score<<endl;
cnt++;
}
iter++;
}
return cnt;
}
void Student::deleteStudent()
{
Student s;
cout<<"请输入要删除的学生学号:"<<endl;
cin>>s.id;
vector<Student>::iterator iter=student.begin();
while (iter!=student.end())
{
if (iter->id==s.id)
{
break;
}
else
iter++;
if (iter!=student.end())
{
student.erase(iter);
cout<<"OK,"<<s.id<<"信息删除完毕。"<<endl;
}
else
{
cout<<"没找到学号为:"<<s.id<<"的学生信息,删除失败。"<<endl;
}
}
}
int main()
{
Student s;
int choice;
cout<<"=-=-=-=-=-=-=-=-=-=-=-=-=-=-欢迎使用学生信息管理系统-=-=-=-=-=-=-=-=-=-=-=-=-=-="<<endl;
begin:
cout<<"1->添加学生信息 2->显示学生信息 3->删除学生信息 "<<endl<<endl;
cout<<" 0-> 退出 "<<endl<<endl;
cout<<"4->从文件添加 5->按成绩排序 6->分数段信息查询"<<endl<<endl;
cin>>choice;
if (choice==1)
{
s.write();
system("cls");
goto begin;
}
else if (choice==2)
{
s.show();
system("pause");
system("cls");
goto begin;
}
else if (choice==3)
{
s.deleteStudent();
system("pause");
system("cls");
goto begin;
}
else if (choice==4)
{
s.red();
system("pause");
system("cls");
goto begin;
}
else if (choice==5)
{
s.sortScore();
system("pause");
system("cls");
goto begin;
}
else if (choice==6)
{
s.a_bScore();
system("pause");
system("cls");
goto begin;
}
else if (choice==0)
{
exit(0);
}
else
{
cout<<"输入非法!!!"<<endl;
system("pause");
goto begin;
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询