小编Ale*_*cki的帖子

如何简洁地填充2D HashMap?

有没有一种简单的方法来填充二维HashMap?在C++中,我可以做以下几点:

std::unordered_map<int,std::unordered_map<int,int>> 2dmap;
//...
2dmap[0][0] = 0;
2dmap[0][1] = 1;
//...
Run Code Online (Sandbox Code Playgroud)

在我的Rust项目中,我试图填写类似的地图:

let mut map: HashMap<i32, HashMap<i32, i32>> = HashMap::new();
//...fill the hash map here.
Run Code Online (Sandbox Code Playgroud)

我能想到的唯一方法是构建每个子地图,然后将它们移动到超级地图中,如下所示:

let mut sub1: HashMap<i32, i32> = HashMap::new();
sub1.insert(0, 0);
sub1.insert(1, 1);
let mut map: HashMap<i32, HashMap<i32, i32>>::new();
map.insert(0, sub1);
Run Code Online (Sandbox Code Playgroud)

有没有更简洁的方法来做到这一点?

上面的代码是我的实际用例的简化,它使用枚举作为索引HashMap:

enum Example {
    Variant1,
    Variant2,
    //...
}
Run Code Online (Sandbox Code Playgroud)

没有变体具有价值.我正在使用此语法进行查询HashMap:

let value = map[Example::Variant1][Example::Variant2];
Run Code Online (Sandbox Code Playgroud)

hashmap rust

7
推荐指数
2
解决办法
644
查看次数

可以从模板参数函数签名中提取类型吗?

有没有办法可以从表单中的函数签名中提取类型,foo(bar)并获得对just foobar.所以,如果我有模板:

template<typename signiture>
class type{
};
Run Code Online (Sandbox Code Playgroud)

签名是什么foo(bar),然后在读取的类中有一个函数

foo function(bar b){
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

我正在接口std::function并发现使用foo(bar)语法更方便,而不是使用多个模板参数,如下所示:

template<typename return_t,param_tps... args>
class type{
    return_t function(param_ps args...){
        return something;
    }
};
Run Code Online (Sandbox Code Playgroud)

如果我能澄清请告诉我?提前致谢.

编辑:为了澄清我感兴趣的是一个具有N个参数的函数,这个参数由特定的类实例指定的任何东西来确定.

编辑2:此问题所依据的代码如下:

using std::function;
template <typename signiture>
class Observer;

template <typename return_t, typename ...args_t>
class Observer<return_t(args_t...)> {
protected:
    using signature = return_t(args_t...);
    typedef function<signature> func_t;
    ~Observer(){}
    func_t what_to_do;
public:
    Observer(Subject<signature>& subject,func_t what):what_to_do(what){
        subject.Attach(what_to_do);
    }
    return_t operator()(args_t... args){
        what_to_do(args...);
    }
};

using …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

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

std :: function到对象的成员函数和对象的生命周期

如果我有一个实例,std::function它绑定到一个对象实例的成员函数,并且该对象实例超出范围而被破坏,我的std::function对象现在被认为是一个坏指针,如果被调用将会失败?

例:

int main(int argc,const char* argv){
    type* instance = new type();
    std::function<foo(bar)> func = std::bind(type::func,instance);
    delete instance;
    func(0);//is this an invalid call
}
Run Code Online (Sandbox Code Playgroud)

标准中是否有某些内容指明应该发生什么?我的预感是它将抛出异常,因为该对象不再存在

编辑:标准是否指定应该发生什么?

是不确定的行为?

编辑2:

#include <iostream>
#include <functional>
class foo{
public:
    void bar(int i){
        std::cout<<i<<std::endl;
    }
};

int main(int argc, const char * argv[]) {
    foo* bar = new foo();
    std::function<void(int)> f = std::bind(&foo::bar, bar,std::placeholders::_1);
    delete bar;
    f(0);//calling the dead objects function? Shouldn't this throw an exception?

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

运行此代码我收到输出值0;

c++ c++11

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

用于构建C++ Google协议缓冲区项目的Makefile

我刚开始使用Google Protocol Buffers,我试图将协议缓冲编译器中的C++输出文件合并到我的项目中.到目前为止,我已经为我的项目使用了一个简单的makefile,它可以构建所有共享相同扩展名的源文件.我使用".cpp"作为源文件,但Google Protocol Buffers将其源代码输出为".pb.cc"文件.我需要能够将两种类型的源文件编译并链接到一个可执行文件中.

我一直在搜索和摆弄我的makefile几个小时,但没有成功.

我当前的Makefile:

PROGRAM_NAME=aserv
CC=gcc
CXX=g++
RM=rm -f
CPPFLAGS=-g  --std=c++14 -O3 -I/usr/local/include/
LDFLAGS=-g -L/usr/local/lib -L/usr/local/lib/boost
LDLIBS= -lrtaudio -pthread -lboost_system -lprotobuf

INSTALL_DIR = /usr/local/bin/

SRCS=$(wildcard *.cpp)
OBJS=$(subst .cpp,.o,$(SRCS))


all: $(PROGRAM_NAME)
 $(PROGRAM_NAME): $(OBJS)
    $(CXX) $(LDFLAGS) -o  $(PROGRAM_NAME) $(OBJS) $(LDLIBS)

depend: .depend

.depend: $(SRCS)
    rm -f ./.depend
    $(CXX) $(CPPFLAGS) -MM $^>>./.depend;

clean:
    $(RM) $(OBJS) $(PROGRAM_NAME) .depend
install:
    cp  $(PROGRAM_NAME) $(INSTALL_DIR)$(PROGRAM_NAME)
uninstall:
    $(RM) $(INSTALL_DIR)$(PROGRAM_NAME)
dist-clean: clean
    $(RM) *~ .depend

include .depend
Run Code Online (Sandbox Code Playgroud)

我还不太精通编写makefile,所以我只是不知道该怎么做才能使这个工作.

如果它有助于我在Ubuntu 16.04 beta上使用GNU make 4.1和gcc 5.3.1

c++ makefile gnu-make protocol-buffers

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

使用operator new和operator delete与自定义内存池/分配器

我正在开发内存池/内存分配器实现,我在一个庄园中设置它,只有一个特殊的"客户端"对象类型可以从池中绘制.客户端可以直接构建到池上,也可以使用池进行动态内存调用,理论上它可以同时执行.我希望能够以一种调用我的池"alloc()"和"free()"函数的方式重载operator newoperator delete,以便获得构造对象所需的内存.

我遇到的一个主要问题是通过调用我编写的pool-> free()函数让我的operator删除能够释放内存.我想出了一个通过将池传递给构造函数并让析构函数执行解除分配工作来修复它的hack.这是好的和花花公子,直到有人需要从这个类继承并覆盖析构函数以满足他们自己的需要,然后忘记执行内存释放.这就是为什么我想把它全部包装在运算符中,所以默认情况下功能隐藏起来并继承.

我的代码在GitHub上:https://github.com/zyvitski/Pool

我对客户的类定义如下:

class Client
{
public:
    Client();
    Client(Pool* pool);
    ~Client();

    void* operator new(size_t size,Pool* pool);
    void operator delete(void* memory);

    Pool* m_pPool;
};
Run Code Online (Sandbox Code Playgroud)

实施是:

Client::Client()
{

}
Client::Client(Pool* pool)
{
    m_pPool = pool;
}
Client::~Client()
{
    void* p = (void*)this;
    m_pPool->Free(&p);
    m_pPool=nullptr;
}
void* Client::operator new(size_t size, Pool* pool)
{
    if (pool!=nullptr) {
        //use pool allocator
        MemoryBlock** memory=nullptr;
        memory = pool->Alloc(size);
       return *memory;
    }
    else throw new std::bad_alloc;
} …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management placement-new new-operator delete-operator

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

使用Regex从字符串中提取数字

我有这个C#代码,我找到了,然后根据我的需求进行了改进,但现在我想让它适用于所有数字数据类型.

    public static int[] intRemover (string input)
    {
        string[] inputArray = Regex.Split (input, @"\D+");
        int n = 0;
        foreach (string inputN in inputArray) {
            if (!string.IsNullOrEmpty (inputN)) {
                n++;
            }
        }
        int[] intarray = new int[n];
        n = 0;
        foreach (string inputN in inputArray) {
            if (!string.IsNullOrEmpty (inputN)) {
                intarray [n] = int.Parse (inputN);
                n++;
            }
        }
        return intarray;
    }
Run Code Online (Sandbox Code Playgroud)

这适用于尝试从字符串中提取整数整数,但我遇到的问题是我使用的正则表达式不是设置为负数的数字或包含小数点的数字.最后我的目标就像我说的那样,制定出一种适用于所有数值数据类型的方法.有人可以帮帮我吗?

.net c# regex string

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

可变参数模板参数包转发中的语法差异

在使用可变参数模板时,我遇到了两种不同的写入调用方式std::forward,但我想知道两种语法之间的实际区别是什么?

template<typename... T>
void one(T&&... args)
{
    foo(std::forward<T&&...>(args...));
}
template<typename... T>
void two(T&&... args)
{
    foo(std::forward<T&&>(args)...);
}
Run Code Online (Sandbox Code Playgroud)

根据我的编译器,这些都是有效的语法,并且在大多数情况下编译器不会抱怨.但是我发现了一些情况,其中一个或另一个是正确的,但编译器没有详细说明原因.

另一个更正确吗?
它们用于不同的目的吗?

c++ variadic-templates perfect-forwarding c++11

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

GCC 5.3.1 C++在编译可变参数模板时失速

我写了以下代码:

#include<array>
#include<type_traits>

namespace math{
    namespace detail{

        template<std::size_t... Is> struct seq{};

        template<std::size_t N, std::size_t... Is>
        struct gen_seq : gen_seq<N-1, N-1, Is...>{};

        template<std::size_t... Is>
        struct gen_seq<0, Is...> : seq<Is...>{};

        template<class T,std::size_t N>
        struct sin_coeffs{
            using array_type = std::array<T,N>;
            constexpr static inline T coeff(std::size_t n){
                return power(-1, n-1) * inverse((T)factorial((2 * n)-1));
            }
            template<std::size_t...NS>
            constexpr static array_type _coeffs(seq<NS...>){
                return {{coeff(NS)...}};
            }
            constexpr static array_type coeffs=_coeffs(gen_seq<N>{});
        };
    }

    template<class T,std::size_t N = max_factorial, class dcy = std::decay_t<T>>
    constexpr std::enable_if_t<std::is_floating_point<dcy>::value,dcy> sin(T x) noexcept{
        constexpr …
Run Code Online (Sandbox Code Playgroud)

c++ gcc variadic-templates c++14

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