I want to clean the initialization of std::map (continued)

Asked 1 years ago, Updated 1 years ago, 353 views

19 hours ago19 hours ago

I want to clean up the initialization of std::map

has been closed and Continued questions has been deleted, so I will preserve them.

It's up to you to think that a pretty code is subjective, but this question is technical enough and there is no other correct way to express it.
I don't mean to argue, but I'm not looking for an answer with only one correct answer, and I've saved it because I feel it's inappropriate to deal with the lack of further questions.

discussion the-scope-of-a-topic

2022-11-05 19:30

3 Answers

As stated in /help/dont-ask in the Help Center, Stack Overflow explicitly prohibits questions that invite feedback.The practice is to narrow down the scope of questions that this site will address and make it easier for the entire community to operate.

Closed Questions clearly asked for feedback on the source code in the questionnaire, so we had no choice but to close it.The first question still had an objective indicator that std::map initialization should be done beautifully using constructors and initialization lists, so I personally don't think this question is enough to close.

If you look at the entire Stack Exchange, please consider posting here because there is a separate site called Code Review Stack Exchange for questions like this one, although it is in English.

Additionally, there are a few additional things to add:

  • If a question or answer is deleted, the contributor will still be able to view it.In other words, logical deletion.You don't have to post the same thing again
  • Keeping some posts that do not follow the topic of the site private has the advantage of making it easier to reach Q&A, which you want to increase the searchability within the site.Even if the deleted post is reposted without any action, the deletion flow will run the same way again.
  • As for closing questions, you can edit the questionnaire to make it easier to convey your intentions and encourage them to reopen, or you can ask for re-opening votes against closing with meta.Especially The first question I think it's open and reasonable, and I feel it's worth throwing at the community.The Help Center /help/reopen-questions is detailed.

Mr. Dameo has indicated several times that he has no intention of discussing it, and I personally would like to respect that, but unfortunately, it is difficult to deal with this series of questions on this site unless you improve or close the post.

*This answer is to re-burn the comment that was attached to the closed question


2022-11-05 19:30

I've seen some better code questions before, and I've posted them before.
So I personally understand why you want to ask these questions, and if you could make the expression "" a little more technical (from the question that started, would it be "simplified without increasing dependency" or "do not use global variables as much as possible?"

"Personally speaking, I think the question that started with the work was too big, so I could only express it as """" a little more beautifully."
I thought it would be easier to answer questions if I subdivided the parts I didn't like/want to improve without looking for a clean code at once.

I'll take your comment and add it.

Ultimately, I think the following is a summary of why the question that originated was considered a problem.

Avoid questions that may be subjective, cause a fire, or require lengthy discussions.This is a place for clear answers!

Quoted from Help Center (emphasis is mine)

I don't think that the question that originated was strongly subjective, but unfortunately it was controversial because it is certain that the answer would change depending on the individual's subjectivity.
For example, you can agree that "beautiful code" and "great code" are very subjective.
"On the other hand, ""pretty code"" is a little more specific, and I think people with certain program experience can share a fluffy image."
However, some people may feel that this fluffy image is as subjective as the beautiful code.
There are two conflicting and different opinions, which can lead to lengthy discussions.

Also, stackoverflow.com is a website that collects questions and answers that people who see questions later can refer to by looking at Q&A.
From this point of view,

There were more comments during the postscript.

I am posting a response to std::map initialization that exists in stackoverflow META (continued).
So

It's up to you to think that a pretty code is subjective, but this question is technical enough and there is no other correct way to express it.
I don't mean to argue, but I'm not looking for an answer with only one correct answer, and I've saved it because I feel it's inappropriate to deal with the lack of further questions.

The answer is my thoughts on .


2022-11-05 19:30

I will post a new answer (before it disappears) to the questions that I asked myself to meet the requirements of this time.
I turned off the time measurement function for static initialization this time.
This is another example.

#include<iostream>
# include <map>
# include <sstream>
# include <iomanip>
# include "magic_enum.hpp" // https://github.com/Neargye/magic_enum/releases/download/v0.8.1/magic_enum.hpp
using namespace std;
enum TCPSTATUS {
    UNKNOWN, ESTABLISHED, SYN_SENT, SYN_RECV, FIN_WAIT1, FIN_WAIT2, TIME_WAIT, CLOSE, CLOSE_WAIT, LAST_ACK, LISTEN, CLOSING,
};
structureKeyGenerator {
    using input_type = TCPSTATUS;
    using value_type=string;
    static value_type generate(input_type i){
            US>stringstream s;
            ss<hex<<uppercase<<setw(2)<setfill('0')<magic_enum::enum_integer(i);
            returns.str();
    }
};
template<typename OriginalIterator, typename Generator>
structure pair_generator_iterator{
    // using original_value_type = typename OriginalIterator::value_type; // for class/structure/union
    using original_value_type=typename remove_pointer<OriginalIterator>::type;//forpointer
    using key_type = typename Generator::value_type;
    usingiterator_category = input_iterator_tag;
    using difference_type=int;
    using value_type=pair<key_type,original_value_type>;
    using pointer = value_type*;
    using reference=value_type&;
    pair_generator_iterator(OriginalIterator):_iter {iter} {}
    value_type operator*()const{
        original_value_type v=*_iter;
        return make_pair(KeyGenerator::generate(v),v);
    }
    pair_generator_iterator&operator++(){_iter++;return*this;}  
    friend bool operator==(const pair_generator_iterator&a, const pair_generator_iterator&b) {return a._iter==b._iter;}
    friend bool operator!=(const pair_generator_iterator&a, const pair_generator_iterator&b) {return!(a==b);}
private:
    OriginalIterator_iter;
};
map<string, TCPSTATUS>table {
    pair_generator_iterator<decltype(magic_enum::enum_values<TCPSTATUS>().begin(), KeyGenerator>(magic_enum::enum_values<TCPSTATUS>().begin(),
    pair_generator_iterator<decltype(magic_enum::enum_values<TCPSTATUS>().end(), KeyGenerator>(magic_enum::enum_values<TCPSTATUS>().end())
};
int main(intargc, char*argv[]){
    for (const auto&p:table) {
        cout<<p.first<"->"<magic_enum::enum_name(p.second)<endl;
    }
    return 0;
}


2022-11-05 19:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.