简体   繁体   English

boost :: flyweight不适用于课堂

[英]boost::flyweight doesn't work for class

first i used flyweight for string which works fine, but when i use flyweight for a struct. 首先,我使用flyweight的字符串工作正常,但是当我使用flyweight的结构。 it doesn't work. 它不起作用。 the first test case for string is: 字符串的第一个测试用例是:

static void testflyweightString()
{
char tmp[0];
vector<boost::flyweight<string>> boost_v;
for(int i=0;i<10000000;i++)
{
sprintf(tmp,"zws_%d",i/1000);
boost_v.pushback(boost::flyweight<string>(tmp));
}
return;
}

then i defined a struct A, some properties in A i used flyweight. 然后我定义了一个结构A,在A中的一些属性我使用了flyweight。 testcase2 is as below: testcase2如下:

static void testflyweightA()
    {
    vector<A> boost_v;
    for(int i=0;i<10000000;i++)
    {
    A a();//here new some A;
    boost_v.pushback(a);
    }
    return;
    }

but it doesn't have any change for memory used whether i used flyweight in A or not. 但是无论我是否在A中使用flyweight,使用的内存都没有任何变化。

First off: 首先:

    A a();//here new some A;

This is: Most vexing parse: why doesn't A a(()); 这是: 最烦人的解析:为什么不使用a(()); work? 工作?


I prepared this test program: 我准备了这个测试程序:

Live On Coliru 生活在Coliru

#include <boost/flyweight.hpp>
#include <vector>
#include <iostream>

static void testflyweightString() {
    std::cout << __FUNCTION__ << "\n";
    std::vector<boost::flyweight<std::string> > boost_v;
    for (int i = 0; i < 10000000; i++) {
        boost_v.emplace_back("zws_" + std::to_string(i/1000));
    }
}

struct A {
    boost::flyweight<std::string> s;
    A(std::string const& s) : s(s) { }
};

static void testflyweightA() {
    std::cout << __FUNCTION__ << "\n";
    std::vector<A> boost_v;
    for (int i = 0; i < 10000000; i++) {
        boost_v.push_back("zws_" + std::to_string(i/1000));
    }
}

int main() {
    testflyweightString();
    testflyweightA();
    std::cout << "Done\n";
}

Its memory usage looked ok using valgrind --tool=massif : 使用valgrind --tool=massif很好地使用valgrind --tool=massif

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM