Element To The Top

I need the best way to put something to the end without re listing for vector container, but not replacing

when I have alot elements my function not that good

this is mine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<vector>
#include<stdio.h>

template<typename unkwn>
void vtot(std::vector<unkwn>&l,size_t i){
    unkwn item=l[i];
        l.erase(l.begin()+i);
        l.push_back(item);
};

int main(int argc,char**args){
    std::vector<const char*>hue;
        hue.push_back("1");
        hue.push_back("2");
        hue.push_back("3");
        hue.push_back("4");

    vtot(hue,0);

    puts(hue[0]);
    puts(hue[1]);
    puts(hue[2]);
    puts(hue[3]);

    return 0;
};
Last edited on
closed account (28poGNh0)
Probably?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<vector>
#include<stdio.h>

template<typename unkwn>
void vtot(std::vector<unkwn>&l,size_t i)
{
    l.push_back(l[i]);
    l.erase(l.begin()+i);
};

int main(int argc,char**args)
{
    std::vector<const char*>hue;
    hue.push_back("1");
    hue.push_back("2");
    hue.push_back("3");
    hue.push_back("4");

    vtot(hue,2);

    puts(hue[0]);
    puts(hue[1]);
    puts(hue[2]);
    puts(hue[3]);

    return 0;
};
not good because nothing really changed
my way is not good because my way will lagg when alot elements
Last edited on
Instead of std::vector maybe you could use std::deque instead?
http://www.cplusplus.com/reference/deque/deque/

Or a list?
http://www.cplusplus.com/reference/list/list/
http://www.cplusplus.com/reference/forward_list/forward_list/

Or write your own container class template?
I don't write own container but if you know another way I'd listen

thanks for deque that much faster than vector
Topic archived. No new replies allowed.