0

I have 2 char arrays like "const char *arr1[ArrSize] = {"Blah1", "Wibble1", "Shrug1"};". For putting them into a vector I found a nice quick solution:

void fillVecTest()
{
    const int ArrSize = 3;
    const char *arr1[ArrSize] = {"Blah1", "Wibble1", "Shrug1"};
    const char *arr2[ArrSize] = {"Blah2", "Wibble2", "Shrug2"};
    std::vector<std::string> vec1(arr1, arr1+ArrSize);
    std::vector<std::string> vec2(arr2, arr2+ArrSize);
    std::vector<std::string>::iterator l_It1Vec1;
    std::vector<std::string>::iterator l_It = vec1.end();
    l_It = find(vec1.begin(), vec1.end(), std::string("Blah1"));
    if(l_It != vec1.end())
    {
      size_t l_pos = l_It - vec1.begin();
      printf("found %s, pos=%u val: %s\n", l_It->c_str(),l_pos, vec2[l_pos].c_str());
    }
 }

Now I thought it should be also possible to put both directly into a map as arr1 is the key and arr2 is the value. I tried some ways but I didn't succeed.

void fillMapTest()
{
    const int ArrSize = 3;
    const char *arr1[ArrSize] = {"Blah1", "Wibble1", "Shrug1"};
    const char *arr2[ArrSize] = {"Blah2", "Wibble2", "Shrug2"};
    std::map<std::string,std::string> map1;//(pair(arr1,arr1), pair(arr1+ArrSize,arr2+ArrSize));
    std::map<std::string,std::string>::iterator l_It1Map1;
    //l_It1Map1 = find(map1.begin(), map1.end(), std::string("Blah1"));
    if(l_It1Map1 != map1.end())
    {
      printf("found key: %s, val: %s\n",l_It1Map1->first.c_str(), l_It1Map1->second.c_str());
    }

}


int _tmain(int /*argc*/, _TCHAR* /*argv[]*/)
{
  fillVecTest();
  fillMapTest();
  return 0;
}

I think that just the commented lines in function "fillMapTest" would need to be solved. Constuctor and find don't work like I want.

Please has any STL expert an idea?

1
  • I forgot to say that I use C++98 and I'm looking first for the constructor solution, if there any. e.g std::map<std::string,std::string> map1(pair(arr1,arr1), pair(arr1+ArrSize,arr2+ArrSize)); Commented May 14, 2013 at 18:07

3 Answers 3

5

The easiest way to write this:

std::map<std::string, std::string> m {
    { "key1", "value1" },
    { "key2", "value2" },
};

This requires your compiler to support initializer lists (a feature of C++11).

Sign up to request clarification or add additional context in comments.

11 Comments

@MatsPetersson I think it's time to assume people have a C++11 compiler (at least C++0x). If they want to support C++03, they should mention it in the question (my personal opinion). Is there a meta discussion about that?
C++11 support is still getting phased in. gcc 4.8 was released a couple weeks ago and still doesn't have all the new features. And it still requires you to explicitly request C++11 with -std=c++11.
@MikeDeSimone, We're getting pretty close with GCC and Clang's current statuses.
@leemes, This isn't in MSVC. The library hasn't been updated for the CTP, and only the CTP has initializer lists.
|
3
std::map<std::string, std::string> m;

for(int i = 0; i < ArrSize; i++) {
    std::pair<std::string, std::string> p =
        std::make_pair(std::string(arr1[i]), std::string(arr2[i]));
    m.insert(p);
}

If you really want to use the map constructor you need an iterator of pairs and the only way(i know) is to use a std::vector<std::pair<std::string, std::string> >::iterator but this seems to be an unneeded extra step to get the same result.

#include <vector>
#include <map>
#include <string>

std::vector<std::pair<std::string, std::string> > values;

for(int i = 0; i < ArrSize; i++) {
    std::pair<std::string, std::string> p =
        std::make_pair(std::string(arr1[i]), std::string(arr2[i]));
    values.push_back(p);
}

std::map<std::string, std::string> m(values.begin(), values.end());

4 Comments

If you have C++11 handy, you can replace std::pair<std::string, std::string> with auto. Or, in either C++, you could just write m.insert(std::make_pair(std::string(arr1[i]), std::string(arr2[i])));
@MikeDeSimone Agreed, in c++11 I would use KerrekSB's solution. But nothing in op's code indicates c++11, and I just wanted to be clear for OP what's happening in the code so I added the extra variable p.
I agree in keeping this answer C++03-compatible. So we have a C++11 and a C++03 solution.
Nice solution and I'll hope it takes me further but the final goal is to get a similar solution just to use the map-constructor like I did with vector.
0
for(int i = 0; i < ArrSize; i++) {
    m.insert(pair<string, string>(arr1[i], arr2[i]));
}

2 Comments

Nice solution but I still hope to find a similar solution just to use the map-constructor like I did with vector.
Now I had time to try it, but get an compile error. error C2665: 'std::pair<_Ty1,_Ty2>::pair' : none of the 3 overloads could convert all the argument types

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.