c++常用容器的操作与使用

set容器内部可以直接进行去重操作不用担心添加的元素会重复,

vector:

1.关于vector遍历:

使用迭代器:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> myVector = {1, 2, 3, 4, 5};

    // 使用迭代器遍历vector
    for (auto it = myVector.begin(); it != myVector.end(); ++it) {
        std::cout << *it << " ";
    }

    return 0;
}

2.声明和初始化:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> myVector = {1, 2, 3, 4, 5};

    // 使用迭代器遍历vector
    for (auto it = myVector.begin(); it != myVector.end(); ++it) {
        std::cout << *it << " ";
    }

    return 0;
}

3.插入删除:

std::vector<int> myVector = {1, 2, 3, 4, 5};

// 在末尾插入元素
myVector.push_back(6);

// 在指定位置插入元素
myVector.insert(myVector.begin() + 2, 10);

// 删除末尾元素
myVector.pop_back();

// 删除指定位置的元素
myVector.erase(myVector.begin() + 3);

4.判断是否为空:

std::vector<int> myVector = {1, 2, 3, 4, 5};

// 检查向量是否为空
bool isEmpty = myVector.empty(); // isEmpty 为 false

5.赋值:

在你的代码中,used 是一个 vector<bool> 类型的变量,用于记录候选数组中的元素是否被使用过。调用 used.resize(candidates.size(), 0) 试图将 used 的大小调整为 candidates 的大小,并初始化所有元素为 0。然而,vector<bool> 是一个特殊的容器,它的元素类型被压缩为一个位,因此不支持普通的数值初始化。如果你试图初始化 vector<bool> 中的元素为非零值,编译器可能会给出警告或错误。

对于 vector<bool>,你可以使用以下方法来初始化所有元素为 false

used.assign(candidates.size(), false);

 6.转移数组:

1)使用标准库中的 std::copy 函数:

#include <vector>
#include <algorithm>

int main() {
    std::vector<int> source = {1, 2, 3, 4, 5};
    std::vector<int> destination(source.size());

    std::copy(source.begin(), source.end(), destination.begin());

    return 0;
}

2)使用迭代器:

#include <vector>
#include <algorithm>

int main() {
    std::vector<int> source = {1, 2, 3, 4, 5};
    std::vector<int> destination(source.begin(), source.end());

    return 0;
}

 

  

map:

1.声明和初始化:

#include <map>

// 声明一个整数到字符串的映射
std::map<int, std::string> myMap;

// 使用初始化列表初始化映射
std::map<int, std::string> myMap = {{1, "One"}, {2, "Two"}, {3, "Three"}};

2.插入和访问元素:

std::map<int, std::string> myMap;

// 插入键值对
myMap[1] = "One";
myMap[2] = "Two";
myMap[3] = "Three";

// 使用 insert() 函数插入键值对
myMap.insert(std::make_pair(4, "Four"));

// 访问元素
std::string value = myMap[2]; // value 等于 "Two"

3.查找元素:

std::map<int, std::string> myMap = {{1, "One"}, {2, "Two"}, {3, "Three"}};

// 使用 find() 查找元素
auto it = myMap.find(2);

if (it != myMap.end()) {
    // 元素找到
    std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;
} else {
    // 元素未找到
    std::cout << "Element not found." << std::endl;
}

4.删除元素:

std::map<int, std::string> myMap = {{1, "One"}, {2, "Two"}, {3, "Three"}};

// 使用 find() 查找元素
auto it = myMap.find(2);

if (it != myMap.end()) {
    // 元素找到
    std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;
} else {
    // 元素未找到
    std::cout << "Element not found." << std::endl;
}

5.遍历映射:

std::map<int, std::string> myMap = {{1, "One"}, {2, "Two"}, {3, "Three"}};

// 使用迭代器遍历映射
for (auto it = myMap.begin(); it != myMap.end(); ++it) {
    std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;
}

// 使用范围-based for 循环遍历映射
for (const auto& pair : myMap) {
    std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}

priority_queue:

#include <queue>
#include <vector>

int main() {
    // 默认是大顶堆
    std::priority_queue<int> maxHeap;

    // 小顶堆的声明和初始化
    std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;

    // 插入元素
    minHeap.push(5);
    minHeap.push(2);
    minHeap.push(8);

    // 访问堆顶元素
    int topElement = minHeap.top();

    // 删除堆顶元素
    minHeap.pop();

    // 获取堆的大小
    size_t size = minHeap.size();

    // 检查堆是否为空
    bool isEmpty = minHeap.empty();

    return 0;
}

注意事项:

在建立一个小根堆的时候如果对象是要比较pair类型的第二个就需要自己手写一个比较函数,如果直接使用greater,那么只比较了pair类型的第一个。

class mycomparison {
    public:
        bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {
            return lhs.second > rhs.second;
        }
    };
// ...............

// 定义一个小顶堆,大小为k
        priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;

在自定义比较顺序的时候需要注意优先级高的点优先出现在堆顶。返回True则表明lhs的优先级高于rhs,返回false则表明lhs的优先级低于rhs。

这里补充一个小技巧:

当我们需要讲一个容器内的数值转移到另一个容器内时可以依靠迭代器来完成:

// 将 map 中的值放入 vector 中
        vector<pair<int, int>> freqVector(map.begin(), map.end());

关于lamda语句在低版本c++环境下的替代:

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

// 定义比较函数对象
struct CompareSecond {
    bool operator()(const std::pair<int, int>& lhs, const std::pair<int, int>& rhs) {
        return lhs.second < rhs.second;
    }
};

int main() {
    std::map<int, int> myMap;
    myMap[1] = 10;
    myMap[2] = 5;
    myMap[3] = 8;
    myMap[4] = 15;

    // 将 map 中的键值对放入 vector 中
    std::vector<std::pair<int, int>> keyValuePairs(myMap.begin(), myMap.end());

    // 对 vector 中的值按照第二个元素(值)从小到大进行排序
    std::sort(keyValuePairs.begin(), keyValuePairs.end(), CompareSecond());

    // 输出排序后的结果
    for (const auto& pair : keyValuePairs) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }

    return 0;
}

我们可以定义一个结构体或者类来构造一个比较函数:

 std::sort(keyValuePairs.begin(), keyValuePairs.end(), [](const auto& lhs, const auto& rhs) {
        return lhs.second < rhs.second;
    });

优先级高的排在前面,优先级低的排在后面。

一些c++语法规范:

1)类名首字母一般为大写字母。

2)运算符重载的标准写法:

class Compare{
        public:
        bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {
            return lhs.second > rhs.second;
        }
    };

使用该函数:

sort(b.begin(), b.end(), Compare());

记得要加括号!!!

3)对pair<int, int>类型的使用规范:

 vector<pair<int, int>> b(a.begin(), a.end());

这里定义了一个队列内部元素为pair类型<int, int>,调用的方法:

result.push_back(b[i].first);

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值