0

2つのベクトルがあります。「ポイント」は、私の元のポイントの配列です。「選ばれた」は「ポイント」から削除されるポイントの集まりです。「選ばれた」からポイントの一意のIDを取得し、それらをイテレータに割り当てて、そのようなポイントを消去したいと思います。しかし、どういうわけか私はそれを行うことはできません。

第二に、私が研究した例では、イテレータがどのように明確なベクトルにリンクされているのか理解できません。あなたの助けを借りて、私がイテレータを理解することを願っています。

#include <StdAfx.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;
struct SPoint
{
    int id;
    int X;
    int Y;
};

vector<SPoint> points;
vector<SPoint> chosen;
vector<SPoint> cleared;

vector<SPoint>::iterator it;

void print_vect(const vector<SPoint> & vect)
{
    for (int i = 0; i < vect.size(); ++i)
    {
        cout << vect[i].id << " (" << vect[i].X << "," << vect[i].Y << ")"<<endl;               
    }           

    cout << endl;   
}
bool compare(double val1, double val2)
{
    return val1 > val2;
}
void sort_points(vector<SPoint> & vect, char command)
{
    bool cmp_result;
    SPoint temp;
    bool sorted=true;
    for (int i = 0; i < vect.size()-1 ; i++)
    {
        sorted=true;
        for (int j = 1; j <= vect.size()-1; j++)
        {
            switch (command) 
            {
                case 'x': 
                    {
                        cmp_result = compare(vect[j-1].X, vect[j].X); 
                        break;
                    }
                case 'y': 
                    {
                        cmp_result = compare(vect[j-1].Y, vect[j].Y); 
                        break;              
                    }               
                case 'i': 
                    {
                        cmp_result = compare(vect[j-1].id, vect[j].id); 
                        break;              
                    }           
            }

            if (cmp_result)
            {
                sorted = false;
                temp = vect[j-1];
                vect[j-1] = vect[j];
                vect[j] = temp;
            }

        }
        if (sorted) 
        {
            cout << "Sorted:" << endl;
            print_vect(vect);           
            break;
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    SPoint temp;
    for (int i = 0; i < 10; i++)
    {
        temp.id = i;
        temp.X = i;
        temp.Y = i;
        points.push_back(temp);
    }

    for (int i = 5; i < 10; i++)
    {
        temp.id = i;
        temp.X = i;
        temp.Y = i;
        chosen.push_back(temp);
    }

    cout << "Points:" << endl;
    print_vect(points);
    cout << endl << endl;

    cout << "Chosen:" << endl;
    print_vect(chosen);

    system("pause");

    vector<SPoint>::iterator it;
    for (int i = 0;i < chosen.size(); i++)
    {       
        //points.erase(it);
    }   

    print_vect(points);
    system("pause");


    print_vect(cleared);
    system("pause");
    return 0;
}
4

2 に答える 2

0

一般に、繰り返し処理されているコンテナーを変更することはお勧めできません。Chosenまた、ソートされていない場合は、O(Points.size() * Chosen.size())(+ 再割り当て)で機能することに注意してください。Points のすべての要素を Chosen のすべての要素 (見つかるか終了するまで) と比較する以外に方法はありません。したがって、Chosen のコンテナーとして使用するset(またはさらに良い) ことをお勧めします。unordered_setの途中から要素を削除したい場合は、多くの再割り当てを行う必要があることにも注意Pointsください。setlist

追加の述語を渡してstd::sort、オブジェクトの特定のフィールドで並べ替えることができます。自分で並べ替えアルゴリズムを再実装する必要はありません。

ベクトルがソートされているかどうかを確認するには、is_sortedメソッド (または、古いコンパイラを使用している場合はadjacent_findこちらのように) を使用できます。

于 2013-01-07T15:16:15.337 に答える
0

抜本的な変更をアドバイスします。std::vectorここでは使用しないでください。std::map代わりに、ポイントidをキーとして使用し、X/Y座標を値として使用します。

using namespace std;
struct SPoint
{
    int X;
    int Y;
};

map<int, SPoint> points;
vector<int> chosen; // only keeps chosen id's, not complete points

void print_points(const map<int, SPoint> & points)
{
    for (map<int, SPoint>::const_iterator i = points.begin(); i != points.end(); ++i)
    {
        cout << i->first << " (" << i->second.X << "," << i->second.Y << ")"<<endl;               
    }           

    cout << endl;   
}

int tmain(int argc, char* argv[])
{
    SPoint temp;
    for (int i = 0; i < 10; i++)
    {
        temp.X = i;
        temp.Y = i;
        points[i] = temp;
    }

    for (int i = 5; i < 10; i++)
    {
        chosen.push_back(i);
    }

    cout << "Points:" << endl;
    print_points(points);
    cout << endl << endl;

    system("pause");

    for (vector<int>::iterator it = chosen.begin(); it != chosen.end(); it++)
    {       
        points.erase(*it); // erase all points with id corresponding to the current value of chosen
    }   

    print_points(points);
    system("pause");
    return 0;
}
于 2013-01-07T15:59:23.847 に答える