2011-08-01 49 views
0
map<string, string> info; 

info["name"] = "something"; 
info["id"] = "5665"; 

在C++中用這種方式初始化這樣的地圖會更好嗎?在C++中用這種方式初始化這樣的地圖會更好嗎?

編輯:我想這樣做沒有任何c + +庫或額外的代碼。事情是這樣的:

info["name", "id"] = {"something", "5665"}; 
+1

更好的意思是什麼? – EvilTeach

+5

'info [「name」] =「something」; //請' – interjay

+0

更好看,類似於{key:value,key:value} – Greta

回答

1

您可以通過從文本文件或與預定義的語法資源文件(例如,key(tab)value)裝載值做到這一點。

您的文字/資源會是這個樣子:

name something 
id  5665 

而你只是在一個循環中分析它一行行。

如果您有正則表達式,您可以使用簡單的(.+)\s(.+),其中組1匹配您的密鑰,組2匹配您的值。

5

使用Boost.Assignment庫,你可以這樣做:

map<string,int> m; 
insert(m)("Bar", 1)("Foo", 2); 

就個人而言,我認爲這傷害了可讀性和你最好做的正常方式。

1

最好的方法是使用C++ 0x初始值設定項列表。但是,如果你的編譯器沒有實現這個功能呢,你可以使用一個類像下面的

/** 
* Class that creates an std::map using a chained list syntax. For example: 
* 
* @code 
* 
* std::map<int, int> SomeMap = MapInit<int, int>(1, 10)(2, 20)(3, 30); 
* 
* @endcode 
* 
* @tparam Key 
* The key data type to be stored in the map. 
* @tparam Type 
* The element data type to be stored in the map. 
* @tparam Traits 
* The type that provides a function object that can compare two element values as sort keys to 
* determine their relative order in the map. This argument is optional and the binary predicate 
* less<Key> is the default value. 
* @tparam Allocator 
* The type that represents the stored allocator object that encapsulates details about the map's 
* allocation and deallocation of memory. This argument is optional and the default value is 
* allocator<pair <const Key, Type> >. 
*/ 
template< class Key, class Type, class Traits = std::less<Key>, 
      class Allocator = std::allocator< std::pair <const Key, Type> > > 
class MapInit 
{ 
    /** Local map instance used to construct the output map */ 
    std::map<Key, Type, Traits, Allocator> myMap_; 

    /* Disallow default construction */ 
    MapInit(); 
    /* Disallow copy construction */ 
    MapInit(const MapInit&); 
    /* Disallow assignment */ 
    MapInit& operator=(const MapInit&); 

public: 
    /** An alias for the type of this object */ 
    typedef typename MapInit<Key, Type, Traits, Allocator>    self_type; 
    /** An alias for the key-value pairs being stored in the map */ 
    typedef typename std::map<Key, Type, Traits, Allocator>::value_type value_type; 

    /** 
    * Constructor that accepts a key and value to be inserted in the map 
    * 
    * @param[in] key 
    * The key value of the element that is to be inserted 
    * @param[in] value 
    * The element to be inserted 
    */ 
    MapInit(const Key& key, const Type& value) 
    { 
    myMap_[key] = value; 
    } 


    /** 
    * Constructor that accepts a key-value pair to be inserted into the map 
    * 
    * @param[in] kvpair 
    * The key-value pair to be inserted 
    */ 
    MapInit(const value_type& kvpair) 
    { 
    myMap_[kvpair.first] = kvpair.second; 
    } 


    /** 
    * Function call operator overload that accepts a key and value to be inserted in the map 
    * 
    * @param[in] key 
    * The key value of the element that is to be inserted 
    * @param[in] value 
    * The element to be inserted 
    * 
    * @return Reference to the operand after inserting the element into the std::map 
    */ 
    self_type& operator()(const Key& key, const Type& value) 
    { 
    myMap_[key] = value; 
    return *this; 
    } 


    /** 
    * Function call operator overload that accepts a key-value pair to be inserted in the map 
    * 
    * @param[in] kvpair 
    * The key-value pair to be inserted 
    * 
    * @return Reference to the operand after inserting the key-value pair into the std::map 
    */ 
    self_type& operator()(const value_type& kvpair) 
    { 
    myMap_[kvpair.first] = kvpair.second; 
    return *this; 
    } 


    /** 
    * Typecast operator to convert the operand to an std::map 
    * 
    * @return The std::map constrcuted by the operand 
    */ 
    operator std::map<Key, Type, Traits, Allocator>() 
    { 
    return myMap_; 
    } 
}; 
3

Boost.Assign可能是你可以用C++ 03得到的最好的。

#include <boost/assign/list_of.hpp> 
#include <map> 
#include <string> 

int main() 
{ 
    using namespace std; 
    map<string, string> info = boost::assign::map_list_of("name", "something")("id", "5665"); 
} 
0

看到我對這個類似問題here的回答。

這將在C++ 0x中超級簡單。

相關問題