cpp中vector的push_back和emplace_back精简小结

push_back 和 emplace_back

网络上讲这两个操作差异的文章很多,这里仅从使用差异分析。

定义

在这里插入图片描述


在这里插入图片描述

假设:

  1. 控制变量:当前vector能够容下push_back和emplace_back的所有元素,没有触发扩容操作。 使用vector.reserve();
  2. push_back和emplace_back操作的对象类型:
    1. 普通变量、普通变量
    2. 普通变量、临时变量
    3. 临时变量、普通变量
    4. 临时变量、临时变量

实验的类Foo

#include <iostream>
#include <vector>

class Foo {
   
   
public:
    // default ctor
    Foo(int value = 0) : value_(value) {
   
   
        std::cout << "Foo(int value = 0)" << std::endl;
    }

    // copy ctor
    Foo(const Foo& foo) : value_(foo.value_) {
   
   
        std::cout << "Foo(const Foo& foo)" << std::endl;
    }

    // move ctor
    Foo(Foo&& foo) : value_(foo.value_) {
   
   
        foo.value_ = 0;
        std::cout << "Foo(Foo&& foo)" << std::endl;
    }

    // copy assignment 
    Foo& operator=(const Foo& foo) {
   
   
        value_ = foo.value_;
        std::cout << 
#include <iostream> // 输入输出流库,用于控制台输入输出 #include <vector> // 向量容器库,用于动态存储课题对象 #include <string> // 字符串库,用于处理文本数据 using namespace std; // 使用标准命名空间,避免重复写std:: // 课题类定义 class Topic { public: string title; // 课题标题 bool isSelected; // 课题是否已被选择 string selectedBy; // 选择该课题的学生姓名 // 构造函数:初始化课题对象 Topic(string t) : title(t), isSelected(false), selectedBy("") {} // 参数t: 课题标题 // 初始化列表: 设置标题为t, 未被选择状态,选择者为空字符串 }; // 课题管理系统类 class TopicManager { private: vector<Topic> topics; // 存储所有课题的向量容器 public: // 增加课题 void addTopic(string title) { topics.emplace_back(title); // 在向量末尾直接构造Topic对象 cout << "课题已添加: " << title << endl; // 输出操作反馈 } // 删除课题 void deleteTopic(int index) { if (index >= 0 && index < topics.size()) { // 验证索引有效性 cout << "课题已删除: " << topics[index].title << endl; // 输出被删除课题 topics.erase(topics.begin() + index); // 从向量中移除指定位置的课题 } else { cout << "无效的课题索引。" << endl; // 索引无效提示 } } // 修改课题 void modifyTopic(int index, string newTitle) { if (index >= 0 && index < topics.size()) { // 验证索引有效性 topics[index].title = newTitle; // 更新课题标题 cout << "课题已修改: " << newTitle << endl; // 输出操作反馈 } else { cout << "无效的课题索引。" << endl; // 索引无效提示 } } // 显示所有课题 void displayTopics() { cout << "当前课题列表:" << endl; // 遍历所有课题 for (size_t i = 0; i < topics.size(); ++i) { cout << i + 1 << ". " << topics[i].title; // 输出序号标题 if (topics[i].isSelected) { // 如果已被选择,显示选择者信息 cout << " (已选择 by " << topics[i].selectedBy << ")"; } cout << endl; // 换行 } } // 选择课题 void selectTopic(int index, string studentName) { if (index >= 0 && index < topics.size()) { // 验证索引有效性 if (!topics[index].isSelected) { // 检查课题是否已被选择 topics[index].isSelected = true; // 标记为已选择 topics[index].selectedBy = studentName; // 记录选择者 cout << "你已选择课题: " << topics[index].title << endl; // 成功提示 } else { // 课题已被选择时提示选择者信息 cout << "该课题已被选择 by " << topics[index].selectedBy << endl; } } else { cout << "无效的课题索引。" << endl; // 索引无效提示 } } // 查看已选课题 void viewSelectedTopics() { cout << "已选课题列表:" << endl; // 遍历所有课题 for (size_t i = 0; i < topics.size(); ++i) { if (topics[i].isSelected) { // 只显示已被选择的课题 cout << topics[i].title << " by " << topics[i].selectedBy << endl; } } } }; // 显示系统菜单 void menu() { cout << "Menu:" << endl; cout << "1. Add Topic" << endl; // 添加课题 cout << "2. Delete Topic" << endl; // 删除课题 cout << "3. Modify Topic" << endl; // 修改课题 cout << "4. Display Topics" << endl; // 显示所有课题 cout << "5. Select Topic" << endl; // 选择课题 cout << "6. View Selected Topics" << endl; // 查看已选课题 cout << "7. Exit" << endl; // 退出系统 } // 主函数 int main() { TopicManager manager; // 创建课题管理器实例 int choice; // 用户选择 do { menu(); // 显示菜单 cout << "请选择操作: "; cin >> choice; // 读取用户选择 cin.ignore(); // 清除输入缓冲区的换行符 switch (choice) { case 1: { // 添加课题 string title; cout << "请输入课题名称: "; getline(cin, title); // 读取整行输入 manager.addTopic(title); break; } case 2: { // 删除课题 int index; cout << "请输入要删除的课题索引: "; cin >> index; // 读取索引 manager.deleteTopic(index - 1); // 索引-1适配内部存储 break; } case 3: { // 修改课题 int index; string newTitle; cout << "请输入要修改的课题索引: "; cin >> index; cin.ignore(); // 清除换行符 cout << "请输入新的课题名称: "; getline(cin, newTitle); // 读取新标题 manager.modifyTopic(index - 1, newTitle); // 索引-1 break; } case 4: // 显示所有课题 manager.displayTopics(); break; case 5: { // 选择课题 int index; string studentName; cout << "请输入要选择的课题索引: "; cin >> index; cin.ignore(); // 清除换行符 cout << "请输入你的名字: "; getline(cin, studentName); // 读取学生姓名 manager.selectTopic(index - 1, studentName); // 索引-1 break; } case 6: // 查看已选课题 manager.viewSelectedTopics(); break; case 7: // 退出系统 cout << "退出系统。" << endl; break; default: // 无效选择处理 cout << "无效的选择。" << endl; break; } } while (choice != 7); // 当选择7时退出循环 return 0; // 程序正常结束 }不用 vector<Topic> topics;利用类的组合或类的继承
06-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ocodotial

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值