标签: std

std::unordered_map::insert 与 std::unordered_map::operator[]

我有一个 unordered_map 类型的容器,如果我想向地图添加元素,我想确认应该使用哪个版本。我希望它用新出现的值覆盖旧值(如果存在),如果不存在则添加它。

我看到 insert 会在元素退出时添加该元素,并且还会返回一对迭代器和 bool,其中 bool 指示插入是否成功。我还看到,operator[] 如果元素不存在则添加该元素,如果存在则覆盖它。

我的问题基本上是我是否应该使用operator[]来达到这个目的,或者是否有任何我没有考虑到的问题。另外,如果我对这些方法的理解有误,请纠正我。

这就是我要做的。Data 是存储类型 int 的作用域枚举

void insertData(const Data _Data, const int _value)
{
 int SC_val = static_cast<int>(_Data);
 //sc val is now the integer value of the Data being added

 //returns a pair of iterator and bool indicating whether the insert was successful
auto ret = baseData.insert(std::pair<int,int>(SC_val,_value));

 if (ret.second == false)
 {//if the insert was not successful(key already exists)
     baseData[ret.first->first] = _value;
 }
}
Run Code Online (Sandbox Code Playgroud)

或者我应该这样做

int index = static_cast<int>(_Data);

baseData[index] = …
Run Code Online (Sandbox Code Playgroud)

c++ methods std

1
推荐指数
1
解决办法
4456
查看次数

我可以将迭代器增加一个整数吗?

std::map<int, int> m;
// initialize m...
//
int n=3;
for (std::map<int, int>::iterator iter = m.begin()+n; iter != m.end(); ++iter)
// Is the above line correct?
{}
Run Code Online (Sandbox Code Playgroud)

我可以按代码中所示的整数递增迭代器吗?

c++ dictionary iterator stdmap std

1
推荐指数
2
解决办法
1338
查看次数

std::condition_variable::notify_all() 仅唤醒线程池中的一个线程

我正在尝试编写一个非常简单的线程池来了解它们在幕后是如何工作的。不过,我遇到了问题。当我使用condition_variable并调用notify_all()时,它只会唤醒池中的一个线程。

其他一切都很好。我已经排队了 900 个工作,每个工作都有不错的负载。唤醒的一个线程会消耗所有这些作业,然后返回睡眠状态。在下一个循环中,这一切都会再次发生。

问题是只有一个线程完成这项工作!我怎么搞砸了这个模板?

线程池.h:

#pragma once

#include <mutex>
#include <stack>
#include <atomic>
#include <thread>
#include <condition_variable>

class ThreadPool
{
friend void __stdcall ThreadFunc();

public:
    static ThreadPool & GetInstance()
    {
        static ThreadPool sInstance;

        return (sInstance);
    }

public:
    void AddJob(Job * job);
    void DoAllJobs();

private:
    Job * GetJob();

private:
    const static uint32_t ThreadCount = 8;

    std::mutex                  JobMutex;
    std::stack<Job *>           Jobs;
    volatile std::atomic<int>   JobWorkCounter;
    std::mutex                  SharedLock;
    std::thread                 Threads[ThreadCount];
    std::condition_variable     Signal;

private:
    ThreadPool();
    ~ThreadPool();

public:
    ThreadPool(ThreadPool const &) = delete;
    void operator …
Run Code Online (Sandbox Code Playgroud)

c++ std condition-variable threadpool

1
推荐指数
1
解决办法
1847
查看次数

std::move 不适用于 RValue 参考函数

在尝试学习 std::move 和右值引用时,我刚刚遇到以下内容:

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<int> vecNumbers;
    vecNumbers.push_back(10);
    vecNumbers.push_back(20);

    foo(std::move(vecNumbers));

    std::cout<<"After Move \n";
    std::cout<<"size:"<<vecNumbers.size()<<"\n";

    return 0;
}

void foo(  std::vector<int> &&value)
{
    std::cout<<"size in Function:"<<value.size()<<"\n";
}
Run Code Online (Sandbox Code Playgroud)

输出

size in Function:2
After Move
size:2
Run Code Online (Sandbox Code Playgroud)

在调用向量上的移动后,我预计大小为 0 ,但这里它仅作为参考移动。有人可以解释一下这里发生了什么吗?

c++ vector std rvalue-reference c++11

1
推荐指数
1
解决办法
820
查看次数

如何将 stl 迭代器与 Eigen 一起使用?

我正在尝试在项目中使用库特征,并且必须对向量进行排序。我尝试遵循文档,它说该库应该以可预测的方式与 STL 迭代器和算法一起工作https://eigen.tuxfamily.org/dox-devel/group__TutorialSTL.html。\n但是当我尝试运行以下测试代码

\n\n
#include <iostream>\n#include <algorithm>\n#include <eigen3/Eigen/Dense>\n#include <eigen3/Eigen/Core>\nint main()\n{\n    Eigen::Array4i v = Eigen::Array4i::Random().abs();\n    std::cout << "Here is the initial vector v:\\n" << v.transpose() << "\\n";\n    std::sort(v.begin(), v.end());\n    std::cout << "Here is the sorted vector v:\\n" << v.transpose() << "\\n";\n    return 0;\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

我收到以下两个错误:

\n\n
error: \xe2\x80\x98Eigen::Array4i\xe2\x80\x99 {aka \xe2\x80\x98class Eigen::Array<int, 4, 1>\xe2\x80\x99} has no member named \xe2\x80\x98begin\xe2\x80\x99\n9 | std::sort(v.begin(), v.end());\n  |             ^~~~~\nerror: \xe2\x80\x98Eigen::Array4i\xe2\x80\x99 {aka \xe2\x80\x98class Eigen::Array<int, 4, 1>\xe2\x80\x99} has no member named \xe2\x80\x98end\xe2\x80\x99\n9 | std::sort(v.begin(), v.end());\n
Run Code Online (Sandbox Code Playgroud)\n\n …

c++ iterator std eigen3

1
推荐指数
1
解决办法
3022
查看次数

x86_64 架构的未定义符号:std:terminate()、typeinfo、operator delete[]、operator new[] 等

我正在学习 Lynda.com C++ 基础培训课程,但某些代码无法编译。下面的例子在编译时会报错:

// new-delete.cpp by Bill Weinman <http://bw.org/>
//   updated 2018-10-27
#include <cstdio>
#include <new>
using namespace std;

constexpr size_t count = 1024;

int main() {
    printf("allocate space for %lu long int at *ip with new\n", count);
    
    // allocate array
    long int * ip;
    
    try {
        ip = new long int [count];
    } catch (std::bad_alloc & ba) {
        fprintf(stderr, "Cannot allocate memory (%s)\n", ba.what());
        return 1;
    }
    
    // initialize array
    for( long int i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

c++ linker compilation std clang

1
推荐指数
1
解决办法
1519
查看次数

堆栈结合了 pop() 和 top() 吗?

std::stack我们用apop()来提取最后一个成员并top()获取它的值。是否有任何快捷方式可以同时执行这两个操作(获取最后一个成员的值并将其踢出)?

c++ stack std

1
推荐指数
1
解决办法
1676
查看次数

`std::sort` 函数不稳定吗?

只是想确认一下我的想法是对还是错。根据定义:

如果具有相同或相同键的两个对象在排序输出中出现的顺序与它们在要排序的输入数组中出现的顺序相同,则称排序算法是稳定的。

现在在标准库的std::sort中,当两个元素相等时返回 false 是必须的。因此,可以肯定地说所使用的排序算法不稳定吗?

c++ sorting std c++17

1
推荐指数
1
解决办法
1305
查看次数

如何在 C++ 中迭代向量的向量?

我想知道是否可以std::vector<std::vector<int>> 通过迭代器访问元素:我无法理解为什么这不能编译:

\n
#include<vector> \n#include<iostream> \n\nstd::vector<std::vector<int>> vec {{1,2},{3,4}} ; \n\n// to access the single vector \nauto it = vec.begin() ; \n\n// to access the element of the vector \nauto iit = it.begin() ; \n\n
Run Code Online (Sandbox Code Playgroud)\n

这是我得到的错误:

\n
prova.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\nprova.cpp:10:15: error: \xe2\x80\x98class __gnu_cxx::__normal_iterator<std::vector<int>*, std::vector<std::vector<int> > >\xe2\x80\x99 has no member named \xe2\x80\x98begin\xe2\x80\x99\n   10 | auto iit = it.begin() ;\n
Run Code Online (Sandbox Code Playgroud)\n

c++ iterator std matrix stdvector

1
推荐指数
1
解决办法
2452
查看次数

如何使 std::map::find 函数区分大小写?

我面试过一家跨国公司。他给了我以下代码并要求我使find()函数区分大小写。我尝试过,但无法理解如何使内置查找函数区分大小写。有没有办法使其区分大小写以仅查找特定的键值?

#include <iostream>
#include <map>
using namespace std;

int main()
{
    map<string, int> mp;
    mp["Test"] = 1;
    mp["test"] = 2;
    mp["TEST"] = 3;
    mp["tesT"] = 4;    

    for (auto it = mp.find("TEST"); it != mp.end(); it++)
    {
        cout << it->first << " " << it->second << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出 :

TEST 3
Test 1
tesT 4
test 2
Run Code Online (Sandbox Code Playgroud)

但我期望输出是:

TEST 3
Run Code Online (Sandbox Code Playgroud)

c++ stdmap std find c++11

1
推荐指数
1
解决办法
154
查看次数