C++: Is there a better way to initialize a vector of pairs?

  • C/C++
  • Thread starter Jamin2112
  • Start date
  • Tags
    Vector
In summary, the conversation discusses different ways to represent a set of data in C++11, using either a vector or a map. The use of a map is recommended as it simplifies the code and follows the concept of KISS (keep it short and simple). Refactoring the code and using typedef can also make it more concise. However, it is also mentioned that from an algorithmic perspective, the use of a map may be costly for a small number of elements.
  • #1
Jamin2112
986
12
Is there a better way to do what I'm trying to do?

Code:
const std::vector<std::pair<std::string, std::string>> derivMap =
   { std::make_pair("x", "1"), std::make_pair("sin(x)", "cos(x)") };

Seems kinda wordy.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
The syntax you are using suggests you are using C++11. (That syntax was not legal in C++98/03.)

Given that, you can replace the std::make_pair(a,b) with {a,b}, which is much more compact.

Code:
const std::vector<std::pair<std::string, std::string>> derivMap =
   { {"x", "1"}, {"sin(x)", "cos(x)"} };

That said, why are you using a vector here? A map would seem to be the better choice:

Code:
const std::map<std::string, std::string> derivMap =
   { {"x", "1"}, {"sin(x)", "cos(x)"} };
 
  • #3
D H said:
That said, why are you using a vector here? A map would seem to be the better choice

Isn't a map overkill if I'm only going to have 10 elements or so? That computation time for a binary search, plus the extra memory, would be useless when I could just iterate across 10 contiguous memory locations.
 
  • #4
Overkill? No! It's the other way around. A map should make your code shorter and simpler. That's the number one goal in programming. There's a name for this concept, KISS (keep it short and simple).
 
  • #5
D H said:
Overkill? No! It's the other way around. A map should make your code shorter and simpler. That's the number one goal in programming. There's a name for this concept, KISS (keep it short and simple).

The concepts I follow are

RTW (Reinvent the Wheel)

and

STOMO (Spend Time on Micro Optimizations)
 
  • #6
Jamin2112 said:
Is there a better way to do what I'm trying to do?

Code:
const std::vector<std::pair<std::string, std::string>> derivMap =
   { std::make_pair("x", "1"), std::make_pair("sin(x)", "cos(x)") };

Seems kinda wordy.

You should probably refactor your code.

Code:
const std::vector<std::pair<std::string, std::string>> derivMap = { 
     std::make_pair("x", "1"), 
     std::make_pair("sin(x)", "cos(x)") 
};

you could also do something along the lines of

Code:
typedef std::pair<std::string, std::string> stringpair;

const std::vector<stringpair> derivMap = {
     std::make_pair("x", "1"), 
     std::make_pair("sin(x)", "cos(x)") 
};
 
  • #7
D H said:
Overkill? No! It's the other way around. A map should make your code shorter and simpler. That's the number one goal in programming. There's a name for this concept, KISS (keep it short and simple).

From an algorithmic point of view, the setup of a red and black binary tree can be an expensive cost to pay for only a few elements.
 

Related to C++: Is there a better way to initialize a vector of pairs?

1. What is the most common way to initialize a vector of pairs in C++?

The most common way to initialize a vector of pairs in C++ is by using the curly brace initializer syntax. This involves enclosing each pair in curly braces and separating them with a comma, and enclosing the entire vector in another set of curly braces.

2. Is there a more efficient way to initialize a vector of pairs in C++?

Yes, there is a more efficient way to initialize a vector of pairs in C++. Instead of using the curly brace initializer syntax, you can use the std::make_pair() function to create and initialize each pair, and then use the std::initializer_list constructor to create the vector of pairs.

3. Can I initialize a vector of pairs with different data types?

Yes, you can initialize a vector of pairs with different data types in C++. This is because the std::pair template class can hold two values of any data type, as long as they are specified within the template parameters.

4. How do I access elements in a vector of pairs in C++?

To access elements in a vector of pairs in C++, you can use the std::vector indexing operator ([]) to specify the index of the element you want to access. You can then use the first and second member variables to access the values in the pair at that index.

5. What happens if I try to initialize a vector of pairs with an odd number of elements?

If you try to initialize a vector of pairs with an odd number of elements, the last element will be discarded. This is because each pair in the vector requires two values, and an odd number of elements cannot be evenly divided into pairs.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
1
Views
991
  • Programming and Computer Science
Replies
3
Views
2K
  • Computing and Technology
Replies
2
Views
89
  • Programming and Computer Science
Replies
6
Views
9K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top