可以用cin来写入vector吗
时间: 2023-08-10 11:14:15 浏览: 137
是的,我们可以使用 `cin` 来读取用户输入并将其添加到vector中。以下是一个示例代码,演示如何使用 `cin` 和 `push_back()` 函数将用户输入添加到vector中:
```c++
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec;
// 读取用户输入并将其添加到vector中
int num;
cout << "Enter numbers (0 to stop): ";
while (cin >> num && num != 0) {
vec.push_back(num);
}
// 输出vector中的元素
cout << "Elements in vector: ";
for (auto it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
return 0;
}
```
在这个示例中,我们定义了一个空的vector `vec`,然后使用 `cin` 读取用户输入并将其添加到vector中,直到用户输入0为止。接着,我们使用迭代器遍历vector,并输出vector中的元素。
相关问题
vector<Student> readUserInfo() { string newUsername, newPassword; cout << "请输入新用户名:"; cin >> newUsername; cout << "请输入新密码:"; cin >> newPassword; vector<Student> userInfos = readUserInfo(); ifstream inFile("user.txt"); if (!inFile.is_open()) { cout << "文件不存在或无法打开!" << endl; return userInfos; } for (Student userInfo : userInfos) { outFile << userInfo.id<< " " << userInfo.password << endl; } outFile.close(); }怎么修改
下面是修改后的代码示例:
```c++
vector<Student> readUserInfo() {
string newUsername, newPassword;
cout << "请输入新用户名:";
cin >> newUsername;
cout << "请输入新密码:";
cin >> newPassword;
vector<Student> userInfos;
ifstream inFile("user.txt");
if (!inFile.is_open()) {
cout << "文件不存在或无法打开!" << endl;
return userInfos;
}
Student userInfo;
while (inFile >> userInfo.id >> userInfo.password) {
userInfos.push_back(userInfo);
}
inFile.close();
ofstream outFile("user.txt", ios::app);
if (!outFile.is_open()) {
cout << "无法打开文件!" << endl;
return userInfos;
}
outFile << userInfo.id << " " << userInfo.password << endl;
outFile.close();
return userInfos;
}
```
修改后的代码中,我们将递归调用改为了循环,使用 while 循环读取文件中的所有用户信息,并将其存入 vector 中。同时,我们在函数末尾使用 ofstream 打开文件,将新的用户名和密码写入文件中,并返回更新后的用户信息 vector。请注意,我们还添加了 outFile 的定义和声明,同时,为了保证每次写入的内容不会覆盖之前的内容,我们使用了 ios::app 模式打开文件。
用c++写一个学籍管理系统
好的,我可以给你提供一个基本的学籍管理系统的代码框架,你可以在此基础上进行修改和完善。
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// 学生信息结构体
struct Student {
string name; // 姓名
int age; // 年龄
string gender; // 性别
string id; // 学号
string major; // 专业
};
// 全局变量,存储所有学生信息
vector<Student> students;
// 读取学生信息
void readStudents() {
ifstream fin("students.txt");
if (!fin) {
cout << "无法打开文件!" << endl;
return;
}
Student s;
while (fin >> s.name >> s.age >> s.gender >> s.id >> s.major) {
students.push_back(s);
}
fin.close();
}
// 写入学生信息
void writeStudents() {
ofstream fout("students.txt");
if (!fout) {
cout << "无法打开文件!" << endl;
return;
}
for (int i = 0; i < students.size(); i++) {
Student s = students[i];
fout << s.name << " " << s.age << " " << s.gender << " " << s.id << " " << s.major << endl;
}
fout.close();
}
// 添加学生信息
void addStudent() {
Student s;
cout << "请输入学生姓名:";
cin >> s.name;
cout << "请输入学生年龄:";
cin >> s.age;
cout << "请输入学生性别:";
cin >> s.gender;
cout << "请输入学生学号:";
cin >> s.id;
cout << "请输入学生专业:";
cin >> s.major;
students.push_back(s);
writeStudents();
cout << "添加成功!" << endl;
}
// 查询学生信息
void queryStudent() {
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
bool found = false;
for (int i = 0; i < students.size(); i++) {
Student s = students[i];
if (s.id == id) {
cout << "姓名:" << s.name << endl;
cout << "年龄:" << s.age << endl;
cout << "性别:" << s.gender << endl;
cout << "学号:" << s.id << endl;
cout << "专业:" << s.major << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到该学生信息!" << endl;
}
}
// 删除学生信息
void deleteStudent() {
string id;
cout << "请输入要删除的学生学号:";
cin >> id;
bool found = false;
for (int i = 0; i < students.size(); i++) {
Student s = students[i];
if (s.id == id) {
students.erase(students.begin() + i);
writeStudents();
cout << "删除成功!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到该学生信息!" << endl;
}
}
// 修改学生信息
void modifyStudent() {
string id;
cout << "请输入要修改的学生学号:";
cin >> id;
bool found = false;
for (int i = 0; i < students.size(); i++) {
Student& s = students[i];
if (s.id == id) {
cout << "请输入学生姓名:";
cin >> s.name;
cout << "请输入学生年龄:";
cin >> s.age;
cout << "请输入学生性别:";
cin >> s.gender;
cout << "请输入学生学号:";
cin >> s.id;
cout << "请输入学生专业:";
cin >> s.major;
writeStudents();
cout << "修改成功!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到该学生信息!" << endl;
}
}
// 显示菜单
void showMenu() {
cout << "------------------------------------" << endl;
cout << "1. 添加学生信息" << endl;
cout << "2. 查询学生信息" << endl;
cout << "3. 删除学生信息" << endl;
cout << "4. 修改学生信息" << endl;
cout << "5. 退出程序" << endl;
cout << "------------------------------------" << endl;
}
int main() {
readStudents();
while (true) {
showMenu();
int choice;
cout << "请输入您的选择:";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
queryStudent();
break;
case 3:
deleteStudent();
break;
case 4:
modifyStudent();
break;
case 5:
cout << "程序已退出!" << endl;
return 0;
default:
cout << "无效的选择,请重新输入!" << endl;
break;
}
}
return 0;
}
```
这个代码框架实现了以下功能:
1. 从文件中读取学生信息;
2. 添加学生信息;
3. 查询学生信息;
4. 删除学生信息;
5. 修改学生信息;
6. 将修改后的学生信息写回文件中。
你可以根据自己的需求进行修改和完善,比如增加更多的学生信息字段,实现更复杂的查询和排序功能等。