首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++模板栈映射/多机

C++模板栈映射/多机
EN

Stack Overflow用户
提问于 2021-06-14 10:00:12
回答 1查看 76关注 0票数 1

我必须测量在不同类型的容器中添加100000 int类型数字所需的时间。有标准的vectorlist --这些都是正确的。我在做mapmultimap时遇到了问题,然后我必须度量为每个容器删除所有这些元素所需的时间。有什么线索吗?

每个容器必须容纳100000 int类型的数字。

  1. 创建堆栈容器(s1)。
  2. 创建一个堆栈容器(s2),它将像向量容器一样管理内存。
  3. 创建一个堆栈容器(s3),它将像列表容器一样管理内存。
  4. 创建一个堆栈容器(s2),它将像地图容器一样管理内存。
  5. 创建了一个堆栈容器(s2),它将管理内存,就像multimap容器。度量添加100000个数字所需的时间。
  6. 度量删除所有容器的元素所需的时间。

更多信息:

  1. 使用标准的C++时间计数方法。时间计数可以这样做:

clock_t begin = clock();// clock_t clock_t end = clock();double elapsed_secs = double(end -clock)/ CLOCKS_PER_SEC;cout << elapsed_secs <

  1. 使用两种自己的堆栈

operation)

  • optimized

  • 未优化(工作正确和无定向,以获得更少的耗时

  • (正确工作,但也可获得较少的耗时操作)

以及基于标准stack<>类的实现。

代码语言:javascript
复制
#include <iostream>
#include <stack>
#include <vector>
#include <list>
#include <map>

using namespace std;


template <class T>
void timeTest(T s)
{
    clock_t begin = clock();
    for (int i = 0; i < 100000; i++) { s.push(i); }
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    cout << elapsed_secs << endl;
}

/*
template <class T, class S>
void timeTest(stack <int, map <int, int>> s)
{
    clock_t begin = clock();
    for (int i = 0; i < 100000; i++) { s.emplace(i, i); }
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    cout << elapsed_secs << endl;
}

template <class T, class S>
void timeTest(stack <pair <int, int>, multimap <int, int>> s)
{
    clock_t begin = clock();
    for (int i = 0; i < 100000; i++) { s.emplace(i, i); }
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    cout << elapsed_secs << endl;
}
*/


int main()
{
    stack <int> s1;
    stack <int, vector <int>> s2;
    stack <int, list <int>> s3;
    //stack <int, map <int, int>> s4;
    //stack<pair<int, int>, multimap<int, int>> s5;


    timeTest(s1);
    timeTest(s2);
    timeTest(s3);
    //timeTest(s4);
    //timeTest(s5);
}
EN

回答 1

Stack Overflow用户

发布于 2021-06-14 11:08:31

找到解决办法

代码语言:javascript
复制
#include <iostream>
#include <time.h>
#include <math.h>
#include <vector>
#include <map>
#include <list>
#include <stack>
using namespace std;

template<class T>
void test(T t,const int I = 1000000) {
    clock_t begin = clock();
    cout << "Uzupelnianie:\t";
    for (int i = 0; i <= I; i++) {
        t.push(i);
    }
    clock_t end = clock();
    double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    cout << elapsed_secs << endl;
    cout << "Usuwanie:\t";
    begin = clock();
    for (int i = 0; i <= I; i++) {
        t.pop();
    }
    end = clock();
    elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
    cout << elapsed_secs << endl;
}


template<class T>
class Mapa {
public:
    map<T, T> mapa;
    using value_type = typename T;
    using reference = typename map<T, T>::reference;
    using const_reference = typename map<T, T>::const_reference;
    using size_type = typename map<T, T>::size_type;
    using container_type = map<T, T>;
    
    
    void push_back(T i) {
        mapa.emplace(i, i);
    }
    void pop_back() {        
        mapa.erase(prev(mapa.end()));
    }
    void empty() {
        cout << "empty\n";
    }
    void back() {
        cout << "back\n";
    }
    void size() {
        cout << "size\n";
    }
    
};

template<class T>
class MultiMapa {
public:
    multimap<T, T> mapa;
    using value_type = typename T;
    using reference = typename multimap<T, T>::reference;
    using const_reference = typename multimap<T, T>::const_reference;
    using size_type = typename multimap<T, T>::size_type;
    using container_type = multimap<T, T>;


    void push_back(T i) {
        mapa.emplace(i, i);
    }
    void pop_back() {
        mapa.erase(prev(mapa.end()));
    }
    void empty() {
        cout << "empty\n";
    }
    void back() {
        cout << "back\n";
    }
    void size() {
        cout << "size\n";
    }

};

int main()
{
    const int N = 1000000;
    stack<int> s1;
    stack<int, vector<int>> s2;
    stack<int, list<int>> s3;
    
    stack<int, Mapa<int>> s4; 
    stack<int, MultiMapa<int>> s5;
    cout << "Stack:\n";
    test(s1, N);

    cout << "Vector:\n";
    test(s2, N);

    cout << "List:\n";
    test(s3, N);

    cout << "Map:\n";
    test(s4,N); 
    
    cout << "MultiMapa:\n";
    test(s5, N);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67968278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档