小编大宝剑*_*大宝剑的帖子

如何释放boost :: unordered_map所拥有的空闲内存?

我添加了大量元素,然后在boost :: unordered_map中将它们全部删除.然后我看到这个程序保存的内存是198MB(大于(64 + 4)*2M),unordered_map大小是0.

然后我测试一个向量,并没有这样的问题.为什么?

#include <iostream>
#include <boost/unordered_map.hpp>

template <int N>
struct big_struct {
    char c[N];
};

int main(void) {
    typedef big_struct<64> data_type;
    typedef boost::unordered_map<int, data_type*> map_type;

    map_type m;

    for (int i = 0; i < 2000 * 1000; i++) {
            m.insert(std::make_pair(i, new data_type));
    }   

    for (map_type::iterator it = m.begin(); it != m.end();) {
            delete it->second;
            it = m.erase(it);
    }   

    std::cout << "finish, map size " << m.size() << std::endl;
    pause();

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

c++ memory unordered-map

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

试图用Boost-Spirit解析SQL语句

我是boost :: spirit的新手.我编写了程序来解析一个SQL语句,比如"select*from table where conditions".它编译失败.报告了大量模板错误.那么,有人会帮助我吗?

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

struct db_select {
    void exec() {}

    std::string filed;
    std::string table;
    std::string condition;
};

std::ostream& operator<<(std::ostream& os, const db_select& se) {
    return os << "filed: " << se.filed << " table: " << se.table << " condition: " << se.condition;
}

template <class Iterator>
struct selecter : qi::grammar<Iterator, db_select (), ascii::space_type> {
    selecter() : selecter::base_type(se) {
            se %= "select" >> +qi::char_ …
Run Code Online (Sandbox Code Playgroud)

c++ boost-spirit

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

获取boost的shared_ptr的指向类型

在我的项目中,我使用 boost::shared_ptr,在一个头文件中,我写道:

typedef boost::shared_ptr<boost::lockfree::spsc_queue<PacketsInput, boost::lockfree::capacity<4096> > > queue_ptr;
Run Code Online (Sandbox Code Playgroud)

在另一个源文件中,我使用它:

std::vector<queue_ptr> v;
for (...)
    v.push_back(boost::make_shared(/* #1 */));
Run Code Online (Sandbox Code Playgroud)

在#1中,我想将queue_ptr的点写入类型,而不是

boost::lockfree::spsc_queue<PacketsInput, boost::lockfree::capacity<4096> >
Run Code Online (Sandbox Code Playgroud)

有多长啊!

但是boost::shared_ptr中没有typedef,我找到的唯一一个typedef:typedef typename boost::detail::sp_element< T >::type element_type;但是我不知道如何使用它。

有什么帮助吗?坦克很多!

c++ boost smart-pointers shared-ptr

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

static-key和__builtin_expect有什么区别?

我读了linux内核文件,这个文件描述了static-key:http://lxr.linux.no/linux+v3.11.1/Documentation/static-keys.txt

那么,static-key和__builtin_expect有什么区别?它们都暗示我们可以使用它们来实现likyly()不太可能().

linux kernel linux-kernel

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

在C或C++中是否有函数实现反向memmem?

我想在HTTP标头中找到"\ r \n\r \n".我知道它在数据包的末尾,所以如果我反向搜索"\ r \n\r \n"我将获得良好的性能.

在C或C++中是否有函数实现反向搜索?如果是这样,哪一个?谢谢.

c c++ algorithm function

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