deleting a pair for reuse

i'm using a pair variable to store some data and every time in the loop i need to delete the contents of the pair for reuse. the type of pair is:
pair <string,vecot< vector< string>>> p;

p.first.clear();
this is working for clearing the first part but for the second part it is not working. How to do this?
First of all you can use a local variable of type std::pair that will be declared inside the loop. So the destructors will be called automatocally.
And what is the problem of using p.second.clear()?
second is a 2d vector and calling p.second.clear() will clear only the contents of the first vector and not the others.
@neutron star
second is a 2d vector and calling p.second.clear() will clear only the contents of the first vector and not the others.


Are you sure?

EDIT: try the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
 
struct A
{
    A() { std::cout << "A::A()\n";  }
    A( const A & ) { std::cout << "A::A( const A & )\n"; }
    ~A() { std::cout << "A::~A()\n"; }
};
 
 
int main()
{
    const size_t N = 3;
    std::vector<std::vector<A>> v( N );
    for ( auto &x : v ) x.push_back( A() );
    
    std::cout << "calling clear\n";
    v.clear();
    
    return 0;
}
Last edited on
ok it worked. i thought it the other way.
Topic archived. No new replies allowed.