Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > <map.h> kak добавить элемент в map container


Автор: brockel 7.10.2006, 14:53
typedef map<string, Tag::Value, less<string> > Map;
Map attributes;

attributes //сюда хочу положитьь новый элемент.

Автор: Daevaorn 7.10.2006, 15:30
brockel,
Код

attributes[ "key" ] = value;


Автор: Damarus 7.10.2006, 15:33
map::insert
Код
// map_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( ) {
   using namespace std;
   map <int, int>::iterator m1_pIter, m2_pIter;

   map <int, int> m1, m2;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );
   m1.insert ( Int_Pair ( 4, 40 ) );

   cout << "The original key values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> first;
   cout << "." << endl;

   cout << "The original mapped values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> second;
   cout << "." << endl;

   pair< map<int,int>::iterator, bool > pr;
   pr = m1.insert ( Int_Pair ( 1, 10 ) );

   if( pr.second == true ) {
      cout << "The element 10 was inserted in m1 successfully." << endl;
   }
   else {
      cout << "Key number 1 already exists in m1\n"
           << "with an associated value of ( (pr.first) -> second ) = " 
           << ( pr.first ) -> second 
           << "." << endl;
   }

   // The hint version of insert
   m1.insert( --m1.end( ), Int_Pair ( 5, 50 ) );

   cout << "After the insertions, the key values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> first;
   cout << "," << endl;

   cout << "and the mapped values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> second;
   cout << "." << endl;

   m2.insert ( Int_Pair ( 10, 100 ) );

   // The templatized version inserting a range
   m2.insert( ++m1.begin( ), --m1.end( ) );

   cout << "After the insertions, the key values of m2 =";
   for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
      cout << " " << m2_pIter -> first;
   cout << "," << endl;

   cout << "and the mapped values of m2 =";
   for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
      cout << " " << m2_pIter -> second;
   cout << "." << endl;
}


map::operator[] 
Код
// map_op_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   typedef pair <const int, int> cInt2Int;
   map <int, int> m1;
   map <int, int> :: iterator pIter;
   
   // Insert a data value of 10 with a key of 1
   // into a map using the operator[] member function
   m1[ 1 ] = 10;

   // Compare other ways to insert objects into a map
   m1.insert ( map <int, int> :: value_type ( 2, 20 ) );
   m1.insert ( cInt2Int ( 3, 30 ) );

   cout  << "The keys of the mapped elements are:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> first;
   cout << "." << endl;

   cout  << "The values of the mapped elements are:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;

   // If the key already exists, operator[]
   // changes the value of the datum in the element
   m1[ 2 ] = 40;

   // operator[] will also insert the value of the data
   // type's default constructor if the value is unspecified
   m1[5];

   cout  << "The keys of the mapped elements are now:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> first;
   cout << "." << endl;

   cout  << "The values of the mapped elements are now:";
   for ( pIter = m1.begin( ) ; pIter != m1.end( ) ; pIter++ )
      cout << " " << pIter -> second;
   cout << "." << endl;
}


Автор: Daevaorn 7.10.2006, 15:34
Или
Код

typedef pair< string, Tag::Value > Pair;
attributes.insert( Pair( key, value ) );

Автор: Мяут 9.10.2006, 18:11
Код

template <class Map, typename Key, typename Value>
void doinsert(Map& m, Key key, Value val) {    
 m.insert(std::pair<Key, Value>(key,val));
}


Зато оригинально  smile 

Автор: JackYF 9.10.2006, 18:20
Мяут, твой пример можно было бы немного изменить.. так, например:

Код

template <typename Key, typename Value, typename GreaterLess>
void doinsert( std::map< Key, Value, GreaterLess >& m, const Key& key, const Value& val )
{    
   m.insert(std::pair<Key, Value>(key,val));
}

template <typename Key, typename Value, typename GreaterLess>
void doinsert( std::multimap< Key, Value, GreaterLess >& m, const Key& key, const Value& val )
{    
   m.insert(std::pair<Key, Value>(key,val));
}


Так, по-моему, больше потенциальных ошибок отсекается... ИМХО.

Добавлено @ 18:21 
Да и функцию назвать как-нибудь типа doMapInsert, что ли...  smile 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)