c++11 delete an object from a container.

Hello.

I have a container in c++11 and i must delete first object and i don't know how...

1
2
3
for (const prob& p : *static_cast<vector<prob>*>(list.listData)){
// i must delete the first object
}


any idea?
Last edited on
What is listData? The cast looks very nasty.

If listData is a pointer to vector<prob> then you can use std::vector::erase() without a for() loop:

http://www.cplusplus.com/reference/vector/vector/erase/

list.listData->erase(list.listData->begin());
Ok i found the ssolution ...

cats.erase(cats.begin());


Yes listData is a pointer to vector<prob>.
I just want to use the first object first and then delete it.
It must be in for loop for other reasons, because sometimes i want to use all objects.
In example bellow i must delete first Cat from the for loop from cats container and the new container bellongs to myThings container.

How i do that?

Truing list.listData->erase(list.listData->begin()); produce error ...
'void*' is not a pointer-to-object type|



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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Cat
{
    string name;
    int age;
};

struct Pencil
{
    float sharpness;
    float length;
};

struct ListOfThings
{
    enum Type
    {
        CatList,
        PencilList
    } type;
    void *listData;
};

vector<Pencil> pencils;
vector<Cat> cats;
vector<ListOfThings> myThings;

void format(const ListOfThings::Type a)
{
swap(a[1],a[2]);// i have error
}

int main()
{
    cats.push_back( {"Joey", 4});
    cats.push_back( {"Mitzi", 6});
    cats.push_back( {"Garfield", 10});

    format(&cats); // error ...............

    pencils.push_back( {1.0f, 17.6f});
    pencils.push_back( {0.6f, 12.1f});
    pencils.push_back( {1.6f, 24.6f});
    pencils.push_back( {0.2f, 18.0f});

    myThings.push_back( {ListOfThings::CatList, &cats});
    myThings.push_back( {ListOfThings::PencilList, &pencils});

    cout << "Hey look, I just found the lists of the things I own. I made them a while ago. Let's take a look at them!\n\n";

    for (const ListOfThings& list : myThings)
    {
        if (list.type == ListOfThings::CatList)
        {
            cout << "Ahh... A list of cats... curious...\n";
            for (const Cat& cat : *static_cast<vector<Cat>*>(list.listData))
{
cats.erase(cats.begin());  // Ok i found the ssolution ...
//  ........
              cout << cat.name << ", age: " << cat.age << '\n';

}
        }
        else if (list.type == ListOfThings::PencilList)
        {
            cout << "Hmm... A bunch of pencils...\n";
            for (const Pencil& pencil : *static_cast<vector<Pencil>*>(list.listData))
            {
                cout << "sharpness: " << pencil.sharpness << ", length: " << pencil.length << '\n';
            }
        }
    }
}
Last edited on
Topic archived. No new replies allowed.