C++学习之 STL的`string`类、动态数组`std::vector`、以及集合类(如`std::set`和`std::map`)阶段练习题

本文介绍了三个C++STL相关的练习题,涉及std::string的字符串操作,std::vector的动态数组管理,以及std::set和std::map的映射功能,旨在帮助初学者巩固相关概念和实践应用。
摘要由CSDN通过智能技术生成

基于C++ STL的string类、动态数组std::vector、以及集合类(如std::setstd::map)的学习,以下是三道代表性的练习题,旨在帮助初学者加深对这些概念的理解和应用。

练习题一:字符串处理

目的:练习使用std::string类的成员函数进行字符串操作。
任务

  1. 创建一个std::string对象,存储一段文本。
  2. 使用std::string的成员函数,完成以下操作:
    • 计算字符串长度。
    • 将字符串的第一个字符改为大写。
    • 找到子字符串"the"在原始字符串中的位置。
    • 替换字符串中的所有"the"为"a"。

示例代码

#include <iostream>
#include <string>

int main() {
    std::string text = "the quick brown fox jumps over the lazy dog";
    
    // 计算字符串长度
    std::cout << "Length of text: " << text.length() << std::endl;
    
    // 将第一个字符改为大写
    text[0] = toupper(text[0]);
    std::cout << "Text after capitalizing first character: " << text << std::endl;
    
    // 查找子字符串"the"的位置
    size_t pos = text.find("the");
    std::cout << "Position of 'the': " << pos << std::endl;
    
    // 替换所有"the"为"a"
    text.replace(text.begin(), text.end(), "the", "a");
    std::cout << "Text after replacing 'the' with 'a': " << text << std::endl;
    
    return 0;
}

练习题二:动态数组操作

目的:练习使用std::vector进行动态数组操作。
任务

  1. 创建一个整型std::vector对象,并初始化为{1, 2, 3, 4, 5}。
  2. 在索引2的位置插入数字6。
  3. 删除索引为1的元素。
  4. 反转整个vector
  5. 使用std::for_each算法打印vector中的所有元素。

示例代码

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    
    // 在索引2的位置插入数字6
    vec.insert(vec.begin() + 2, 6);
    
    // 删除索引为1的元素
    vec.erase(vec.begin() + 1);
    
    // 反转整个vector
    std::reverse(vec.begin(), vec.end());
    
    // 使用std::for_each打印所有元素
    std::for_each(vec.begin(), vec.end(), [](int num) {
        std::cout << num << " ";
    });
    
    std::cout << std::endl;
    return 0;
}

练习题三:映射和多重映射

目的:练习使用std::mapstd::multimap进行映射操作。
任务

  1. 创建一个std::map对象,存储员工的ID和姓名。
  2. std::map中添加几条记录。
  3. 查找特定ID的员工姓名。
  4. 修改一个员工的姓名。
  5. 创建一个std::multimap对象,存储单词与其出现次数的映射。

示例代码

#include <iostream>
#include <map>
#include <multimap>
#include <string>

int main() {
    // 员工ID和姓名的map
    std::map<int, std::string> employees;
    employees[101] = "John";
    employees[102] = "Jane";
    employees[103] = "Jim";
    
    // 查找特定ID的员工姓名
    auto it = employees.find(102);
    if (it != employees.end()) {
        std::cout << "Employee 102 is " << it->second << std::endl;
    }
    
    // 修改员工姓名
    employees[102] = "Alex";
    
    // 单词与其出现次数的multimap
    std::multimap<std::string, int> wordCount;
    wordCount.insert({"the", 2});
    wordCount.insert({"quick", 1});
    wordCount.insert({"the", 1}); // multimap允许键重复
    
    // 打印multimap中的所有键值对
    for (const auto& pair : wordCount) {
        std::cout << pair.first << " appears " << pair.second << " times" << std::endl;
    }
    
    return 0;
}
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值