开发者

What would be a nicer way to initialize a map like this in C++?

map<string, string> info;

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

What would be a nicer way to initialize a map like this in C++?

EDIT: I want to开发者_Python百科 do this without any c++ libraries or extra code. Something like this:

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


Using the Boost.Assignment library, you can do this:

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

Personally, I think this hurts readability and you're better off doing it the normal way.


Take a look at C++0x initializer list feature. Much nicer :)


Boost.Assign is probably the nicest you can get with 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");
}


You could do this by loading the values from a text file or a resources file with a predefined syntax (for example, key(tab)value).

Your text / resource would look something like this:

name    something
id      5665

And you'd just parse it line by line in a loop.

If you had regular expressions at your disposal, a simple (.+)\s(.+) would do, where group 1 matches your key, and group 2 matches your value.


The best way is to use a C++0x initializer list. But if your compiler doesn't implement this feature yet, you can use a class like the following

/**
 * 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_;
  }
};


Boost assign is one of neatest way of doing STL containers initialization:

Some good example for std::vector, set, map can be found here:

http://www.boost.org/doc/libs/1_47_0/libs/assign/doc/index.html#examples

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜