std::map Initialization Questions [Closed]

Asked 1 years ago, Updated 1 years ago, 367 views

Do you want to improve this question?Update your question, Edit this post to be answered with facts and quotes.

Closed to 55 minutes ago.

55 minutes ago

I want to clean up the initialization of std::map
Now that the question in has been closed, I would like to know how you feel about construct_table_with_iterator and construct_table_with_move in the code below.

#include<iostream>
# include <map>
# include <string>
# include <vector>
# include <chrono>
using namespace std;
using namespace std::chrono;
structureKeyGenerator {
    using input_type=int;
    using value_type=string;
    static value_type generate (input_type i) {return to_string(i);}
};
template<typename OriginalIterator, typename Generator>
structure pair_generator_iterator{
    using original_value_type = typename OriginalIterator::value_type;
    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;
};
int check_map(const map<string, int>&m){
    int total = 0;
    for(const auto&e:m){
        total + = e.first.length() + e.second;
    }
    return total;
}
int construct_table_with_iterator(constructor<int>&iv){
    map<string, int>m(
        pair_generator_iterator<vector<int>::const_iterator, KeyGenerator>(iv.begin()) ,
        pair_generator_iterator<vector<int>:const_iterator, KeyGenerator>(iv.end()));
    return check_map(m);
}
int construct_table_with_move(constructor<int>&iv){
    map<string, int>_m;
    for(inti=0;i<iv.size();++i){
        _m [to_string(i)] = i;
    }
    map<string, int>m=move(_m);
    return check_map(m);
}
int main() {
    constint N = 1000000;
    vector<int>iv(N);
    for(inti=0;i<N;++i){
        iv[i] = i;
    }
    auto p1 = chrono::high_resolution_clock::now();
    auto result1 = construct_table_with_iterator(iv);
    auto p2 = chrono::high_resolution_clock::now();
    auto p3 = chrono::high_resolution_clock::now();
    auto result2=construct_table_with_move(iv);
    auto p4 = chrono::high_resolution_clock::now();
    cout<<"iterator generator:"<duration_cast<microseconds>(p2-p1).count()<endl;
    cout<<"move:"<duration_cast<microseconds>(p4-p3).count()<endl;
    return result1!=result2;
}

I would appreciate it if you could give me your feedback or like other people's answers.
I will not close this question.
In addition to your feedback, I would appreciate it if you could provide me with a code that I think would be better.

Add

Existing content has not been resolved.This question contains the equivalent of the answer to the previous question.Don't talk about anything other than the question.

c++

2022-10-26 00:00

1 Answers

I don't think I'll write it to anyone, so I'll write down my thoughts.

construct_table_with_iterator

I think it's only useful for initialization purposes, not for iterator as an accessory to the container, but it doesn't require a list for initialization and doesn't incur the cost of moving std::map, so it's qualitatively faster.As for the speed, I think it depends on the library.Considering maintenance costs, it is more cost effective and in some cases it may be negative.

construct_table_with_move

It is written simply and is easy to understand and resistant to change.The std::map is in the form of two instances being declared, but it is moving, so the cost is minimized.Optimization is easy to benefit from.On the other hand, if the library has a costly implementation of moving std::map (for special environments, processing systems, and data structures), it will be greatly affected.

The results are watery, so I can't post them, but they are qualitatively
regardless of the treatment system. Compiler Explorer( 11): construct_table_with_iterator>construct_table_with_move
wandbox( 22): construct_table_with_iterator<construct_table_with_move
Your environment: construct_table_with_iterator ~ construct_table_with_move
It looks like this (the bigger one is faster).
Since the wandbox had the least blurring in the measurement results, I expect that the results will probably be similar in many cases.

I don't really feel the need to measure seriously in this situation, so please forgive me (you can easily check it yourself).

11 https://godbolt.org/
22 https://wandbox.org/


2022-10-26 00:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.