0

这是代码:

#include <iostream>
#include <string>
#include <map>
#include <stdexcept>
#include <boost/ptr_container/ptr_vector.hpp>

struct TestStruct
{
    std::string str1;
    int var1;
};

struct Config
{
    // Map
    typedef std::map< std::string, boost::ptr_vector<struct TestStruct> > testMap;
};

void foo(Config& config)
{
    if (config.testMap.empty())
    {
        std::cout << "It worked!" << std::endl;
    }
    else
    {
        std::cout << "It didn't work!" << std::endl;
    }
    return;
}

int testMain(void)
{
    Config config;

    foo(config);

    return;
}

int main(void)
{
    try
    {
        return testMain(/*argc, argv*/);
    }
    catch(std::exception& err)
    {
        std::cerr << "Error running program: " << err.what() << std::endl;
        return 1;
    }
    catch(...)
    {
        std::cerr << "Program failed with an unknown exception." << std::endl;
        return 1;
    }
}

我是地图新手 - 以前从未使用过。我在网上找到了很多如何使用它们的例子。不幸的是,我似乎无法理解它们的更高级示例。

我想做的是创建一个带有键(std::string)和值(boost::ptr_vector<struct>)的地图。

我打算首先声明并成功传递它。然后我想试着弄清楚如何填满它。

我遇到了一个非常模糊的错误,我不知道如何解释它。

关于我在“使用”中做错了什么有什么建议testMap吗?

另外,有人可以提供一些简单的例子来说明我如何填充地图。

假设我想要一个 的键a和一个 的值str1 = "hello", var1 = 10。我该怎么做?

跟进问题:关于Kerrek SB 留下的答案。

如果我执行以下操作...

std::string key   = "a";
TestStruct value = {"hello", 10};
config.testMap[key] = value;

我收到以下错误:

 error: no match for 'operator=' in 'config->Config::testMap.std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = boost::ptr_vector<TestStruct, boost::heap_clone_allocator, std::allocator<void*> >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::ptr_vector<TestStruct, boost::heap_clone_allocator, std::allocator<void*> > > >](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& key)))) = value'
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note: candidates are: boost::ptr_vector<T, CloneAllocator, Allocator>& boost::ptr_vector<T, CloneAllocator, Allocator>::operator=(std::auto_ptr<boost::ptr_vector<T, CloneAllocator, Allocator> >) [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note:                 boost::ptr_vector<T, CloneAllocator, Allocator>& boost::ptr_vector<T, CloneAllocator, Allocator>::operator=(boost::ptr_vector<T, CloneAllocator, Allocator>) [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]

相反,如果我执行该.insert()方法,则会收到以下错误:

instantiated from here
/opt/csw/gcc4/lib/gcc/sparc-sun-solaris2.8/4.3.2/../../../../include/c++/4.3.2/bits/stl_pair.h:106: error: no matching function for call to 'boost::ptr_vector<TestStruct, boost::heap_clone_allocator, std::allocator<void*> >::ptr_vector(const TestStruct&)'
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:50: note: candidates are: boost::ptr_vector<T, CloneAllocator, Allocator>::ptr_vector(typename boost::ptr_sequence_adapter<T, std::vector<void*, Allocator>, CloneAllocator>::size_type, const typename boost::ptr_sequence_adapter<T, std::vector<void*, Allocator>, CloneAllocator>::allocator_type&) [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note:                 boost::ptr_vector<T, CloneAllocator, Allocator>::ptr_vector(std::auto_ptr<boost::ptr_vector<T, CloneAllocator, Allocator> >) [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note:                 boost::ptr_vector<T, CloneAllocator, Allocator>::ptr_vector(const typename boost::ptr_sequence_adapter<T, std::vector<void*, Allocator>, CloneAllocator>::allocator_type&) [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:45: note:                 boost::ptr_vector<T, CloneAllocator, Allocator>::ptr_vector() [with T = TestStruct, CloneAllocator = boost::heap_clone_allocator, Allocator = std::allocator<void*>]
/usr/local/boost/boost_1_38_0_gcc4/include/boost/ptr_container/ptr_vector.hpp:35: note:                 boost::ptr_vector<TestStruct, boost::heap_clone_allocator, std::allocator<void*> >::ptr_vector(const boost::ptr_vector<TestStruct, boost::heap_clone_allocator, std::allocator<void*> >&)

跟进:

根据我的研究,这是不可能的。

您不能ptr_vector在地图中使用 a。(所以我被告知)由于所有权/复制问题?

“当您尝试复制 ptr_vector 时会发生什么,这必然会发生在地图内?指针容器模拟指针的独占所有权。

你可以用 C++0x 和 std::move 做到这一点,但这对你没有帮助。”

谁能提供一个反例?

4

2 回答 2

1

您的班级Config不包含任何成员!(只是一个 typedef。)也定义一个成员:

struct Config
{
    typedef std::map<std::string, boost::ptr_vector<TestStruct> > testMap_type;

    testMap_type testMap; // member
};

此外,在 C++ 中无需说struct TestStruct,只需说TestStruct.

添加元素:

std::string key = "xxx";
TestStruct  val = { "hello", 10 };

// Insert with []-operator
config.testMap[key] = val;

// Insert with insert():
config.testMap.insert(std::pair<std::string, TestStruct>(key, val)); // alternative

编辑:对不起,我歪曲了你的实际数据结构。这是一个带有的示例std::vector

typedef std::map<std::string, std::vector<int>> MyMap;
MyMap m;

m["hello"].push_back(1);
m["hello"].push_back(2);
m["world"].push_back(3);

在您的情况下,您可以说config.testMap[key].push_back(val),或者您可以创建一个新向量:

boost::ptr_vector<TestStruct> new_v;
// populate new_v;
config.testMap.insert(std::pair<std::string, boost::ptr_vector<TestStruct>>(key, new_v);
于 2011-07-22T18:53:11.743 回答
1

根据我的研究,这是不可能的。

您不能在地图中使用 ptr_vector。(所以我被告知)由于所有权/复制问题?

“当您尝试复制 ptr_vector 时会发生什么,这必然会发生在地图内?指针容器模拟指针的独占所有权。

你可以用 C++0x 和 std::move 做到这一点,但这对你没有帮助。”

于 2012-05-17T18:53:31.270 回答