Search Results

Search found 9 results on 1 pages for 'stdmap'.

Page 1/1 | 1 

  • Is there a way for std::map to "edit" values like a predicate for the key?

    - by Marlon
    I am wondering if it is possible to create something like a predicate for a std::map for all of its values so I don't have to edit the values before I insert them into the map. What I would like is something like this: mymap["username"] = " Marlon "; // notice the space on both sides of my name assert(mymap["username"] == "Marlon"); // no more whitespace The context is I am creating a std::map for a .ini file and I would like it to automatically remove leading/trailing whitespace from the values when I want to retrieve them. I've already created a predicate to ignore casing and whitespace from the key so I want to know if it is possible to do the same for the value.

    Read the article

  • Can't access a map member from a pointer

    - by fjfnaranjo
    Hi. That's my first question :) I'm storing the configuration of my program in a Group->Key->Value form, like the old INIs. I'm storing the information in a pair of structures. First one, I'm using a std::map with string+ptr for the groups info (the group name in the string key). The second std::map value is a pointer to the sencond structure, a std::list of std::maps, with the finish Key->Value pairs. The Key-Value pairs structure is created dynamically, so the config structure is: std::map< std::string , std::list< std::map<std::string,std::string> >* > lv1; Well, I'm trying to implement two methods to check the existence of data in the internal config. The first one, check the existence of a group in the structure: bool isConfigLv1(std::string); bool ConfigManager::isConfigLv1(std::string s) { return !(lv1.find(s)==lv1.end()); } The second method, is making me crazy... It check the existence for a key inside a group. bool isConfigLv2(std::string,std::string); bool ConfigManager::isConfigLv2(std::string s,std::string d) { if(!isConfigLv1(s)) return false; std::map< std::string , std::list< std::map<std::string,std::string> >* >::iterator it; std::list< std::map<std::string,std::string> >* keyValue; std::list< std::map<std::string,std::string> >::iterator keyValueIt; it = lv1.find(s); keyValue = (*it).second; for ( keyValueIt = keyValue->begin() ; keyValueIt != keyValue->end() ; keyValueIt++ ) if(!((*keyValueIt).second.find(d)==(*keyValueIt).second.end())) return true; return false; } I don't understand what is wrong. The compiler says: ConfigManager.cpp||In member function ‘bool ConfigManager::isConfigLv2(std::string, std::string)’:| ConfigManager.cpp|(line over return true)|error: ‘class std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >’ has no member named ‘second’| But it has to have the second member, because it's a map iterator... Any suggestion about what's happening? Sorry for my English :P, and consider I'm doing it as a exercise, I know there are a lot of cool configuration managers.

    Read the article

  • How can i estimate memory usage of stl::map?

    - by Drakosha
    For example, I have a std::map with known sizeof(A) and sizefo(B), while map has N entries inside. How would you estimate its memory usage? I'd say it's something like (sizeof(A) + sizeof(B)) * N * factor But what is the factor? Different formula maybe? Update: Maybe it's easier to ask for upper bound?

    Read the article

  • Custom types as key for a map - C++

    - by Appu
    I am trying to assign a custom type as a key for std::map. Here is the type which I am using as key. struct Foo { Foo(std::string s) : foo_value(s){} bool operator<(const Foo& foo1) { return foo_value < foo1.foo_value; } bool operator>(const Foo& foo1) { return foo_value > foo1.foo_value; } std::string foo_value; }; When used with std::map, I am getting the following error. error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Foo' (or there is no acceptable conversion) c:\program files\microsoft visual studio 8\vc\include\functional 143 If I change the struct like the below, everything worked. struct Foo { Foo(std::string s) : foo_value(s) {} friend bool operator<(const Foo& foo,const Foo& foo1) { return foo.foo_value < foo1.foo_value; } friend bool operator>(const Foo& foo,const Foo& foo1) { return foo.foo_value > foo1.foo_value; } std::string foo_value; }; Nothing changed except making the operator overloads as friend. I am wondering why my first code is not working? Any thoughts?

    Read the article

  • std::map default value for build-in type

    - by Qifa Zhao
    Recently, I was confused by the std::map operator[] function. In the MSDN library, it says: "If the argument key value is not found, then it is inserted along with the default value of the data type." I tryed to search much more exactly explanation for this issue. For example here: std::map default value In this page, Michael Anderson said that "the default value is constructed by the default constructor(zero parameter constructor)". Now my quest comes to this:"what the default value for the build-in type?". Was it compiler related? Or is there a standard for this issue by the c++ stardard committee? I did a test on visual studio 2008 for the "int" type, and found the "int" type is construted with the value 0.

    Read the article

  • Java equivalent of C++ std::map?

    - by Rudiger
    I'm looking for a Java class with the characteristics of C++ std::map's usual implementation (as I understand it, a self-balancing binary search tree): O(log n) performance for insertion/removal/search Each element is composed of a unique key and a mapped value Keys follow a strict weak ordering I'm looking for implementations with open source or design documents; I'll probably end up rolling my own support for primitive keys/values. This question's style is similar to: Java equivalent of std::deque, whose answer was "ArrayDeque from Primitive Collections for Java".

    Read the article

  • Why does std::map operator[] create an object if the key doesn't exist?

    - by n1ck
    Hi, I'm pretty sure I already saw this question somewhere (comp.lang.c++? Google doesn't seem to find it there either) but a quick search here doesn't seem to find it so here it is: Why does the std::map operator[] create an object if the key doesn't exist? I don't know but for me this seems counter-intuitive if you compare to most other operator[] (like std::vector) where if you use it you must be sure that the index exists. I'm wondering what's the rationale for implementing this behavior in std::map. Like I said wouldn't it be more intuitive to act more like an index in a vector and crash (well undefined behavior I guess) when accessed with an invalid key? Refining my question after seeing the answers: Ok so far I got a lot of answers saying basically it's cheap so why not or things similar. I totally agree with that but why not use a dedicated function for that (I think one of the comment said that in java there is no operator[] and the function is called put)? My point is why doesn't map operator[] work like a vector? If I use operator[] on an out of range index on a vector I wouldn't like it to insert an element even if it was cheap because that probably mean an error in my code. My point is why isn't it the same thing with map. I mean, for me, using operator[] on a map would mean: i know this key already exist (for whatever reason, i just inserted it, I have redundancy somewhere, whatever). I think it would be more intuitive that way. That said what are the advantage of doing the current behavior with operator[] (and only for that, I agree that a function with the current behavior should be there, just not operator[])? Maybe it give clearer code that way? I don't know. Another answer was that it already existed that way so why not keep it but then, probably when they (the ones before stl) choose to implement it that way they found it provided an advantage or something? So my question is basically: why choose to implement it that way, meaning a somewhat lack of consistency with other operator[]. What benefit do it give? Thanks

    Read the article

  • C++: Accessing std::map keys and values

    - by Jay
    How do you access an std::vector of the keys or values of an std::map? Thanks. Edit: I would like to access the actual elements, not just copies of their contents. essentially I want a reference, not a copy. This is essentially what I am wanting to do: std::map<std::string, GLuint> textures_map; // fill map glGenTextures( textures_map.size(), &textures_map.access_values_somehow[0] );

    Read the article

1