STL study notes in C++-mixed use of various containers


STL

Overview

Recently, I used a more complicated data storage method when I was working on a thing. In fact, the complexity is mainly because of my own cooking. For the big guys, I can see it at a glance. My final goal is to achieve the intersection of two coordinate sets. Because I want to use STL to achieve this, I used a container to store all kinds of data in the early stage.

Data storage format

First, a structure is needed to store the point set. Where x and y are coordinates and label is the point label.


typedef struct point
{
    int x;
    int y;
    int label;
    point()
    {
        x = -1;
        y = -1;
        label = -1;
    }
} Point;

Then define a RegionObject

map<int, vector<Point>> RegionObject;
Since I have not used too complex storage structure before, it is difficult to assign values ​​to RegionObject (mainly too). Is there any reference for assignment of similar structures on the Internet, so I Just fumble and try. Finally, several assignment methods were found.

Method 1 : Use Point to transfer, and then vector=vector


//创建一个中转变量
Point temp;
//初始化temp
temp.x = 1;
temp.y = 1;
temp.label = 1;
//创建一个新的vector并利用temp初始化
vector<Point> vec1;
vec1.push_back(temp);
//传值
RegionObject[0]=vec1;

Method 2 : Use Point to transfer, and then push_back in


//创建一个中转变量
Point temp;
//初始化temp
temp.x = 1;
temp.y = 1;
temp.label = 1;
//传值
RegionObject[0].push_back(temp);

Later, I came up with other ways, because the previous code has been written in this way, there is no change. To figure out these composite containers, the premise is to be clear about a single container.

Find the intersection of sets

At first, I thought of using the set_intersection function to solve directly, but at the beginning I only found one version of the function that has five parameters.


set_intersection(vec3.begin(), vec3.end(), vec4.begin(), vec4.end(), inserter(vec, vec.begin()));

It occurred to me that the function did not specify the method of intersection for a single element, and there was no judgment parameter indicating that the default comparison method was called. I guess it was judged by "==", so I defined it inside the structure A "==" operator overloaded function.


 bool operator==(point a1){
      if(this->x == a1.x && this->y == a1.y) return true;
      else return false;
  }

Then use the above function to find the intersection and find that there is a problem. The "<" sign is prompted in the error report. Therefore, the "==" I guess may be a bit problematic. After that, I did not go to the source code, but found that the intersection function actually has an overloaded version, which can add a custom comparison function, so I customized a cmp function to realize the coordinate class (or Is the function of multidimensional vector) intersection.

Implementation

In fact, his specific implementation is much more complicated than the following, here is what I simplified.


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
#include <numeric>
#include <map>
using namespace std;

typedef struct a
{
    int x;
    int y;
    a()
    {
        x = 0;
        y = 0;
    }
} aaa;

bool cmp(aaa &ab, aaa &ac)
{
    if (ab.x == ac.x && ab.y == ac.y)
        return true;
    else
        return false;
}
int main()
{
    aaa a1, a2, a3;
    a1.x = 1;
    a1.y = 1;
    a2.x = 2;
    a2.y = 2;
    a3.x = 3;
    a3.y = 3;

    vector<aaa> vec1;
    vector<aaa> vec2;
    vector<aaa> vec;
    vec1.push_back(a1);
    vec1.push_back(a2);
    vec2.push_back(a3);
    vec2.push_back(a1);
    vec2.push_back(a2);
    map<int, vector<aaa>> map1;
    map1[0] = vec1;
    map1[1] = vec2;
    map<int, vector<aaa>>::iterator iter = map1.begin();
    vector<aaa> vec3 = iter->second;
    iter++;
    vector<aaa> vec4 = iter->second;

    set_intersection(vec3.begin(), vec3.end(), vec4.begin(), vec4.end(), inserter(vec, vec.begin()),cmp);
    cout << vec.size() << endl;
    cout << vec[0].x <<" "<<vec[0].y<< endl;
    cout << vec[1].x <<" "<<vec[1].y<< endl;
    getchar();
    return 0;
}

Guess you like

Origin blog.51cto.com/15069472/2577329