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

  • Context: C/C++ 
  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    Vector
Click For Summary

Discussion Overview

The discussion revolves around the initialization of a vector of pairs in C++, specifically exploring alternatives to using std::make_pair for creating a vector of string pairs. Participants consider the appropriateness of using a vector versus a map for storing these pairs, discussing the implications of each choice in terms of code simplicity and performance.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants suggest that using the syntax {a, b} is more compact than std::make_pair(a, b) for initializing pairs in C++11.
  • Others argue that using a map instead of a vector could be more appropriate, as it may simplify the code.
  • One participant questions whether a map is overkill for a small number of elements, suggesting that the overhead of a map may not be justified for only 10 elements.
  • Another participant counters that a map can make the code shorter and simpler, emphasizing the KISS principle (Keep It Simple, Stupid).
  • Some participants express concern about the algorithmic cost of using a map, particularly the setup of a red-black tree for a small dataset.
  • A later reply proposes using a typedef for pairs to potentially improve code readability while still using a vector.

Areas of Agreement / Disagreement

Participants express differing opinions on whether a vector or a map is the better choice for this scenario, indicating that multiple competing views remain. There is no consensus on the optimal approach to initializing the vector of pairs.

Contextual Notes

Participants highlight considerations regarding performance and code simplicity, but do not resolve the trade-offs between using a vector and a map.

Jamin2112
Messages
973
Reaction score
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
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)"} };
 
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.
 
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).
 
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)
 
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)") 
};
 
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.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K