简体   繁体   中英

Dividing a vector into smaller vectors upon its properties

I want to write something in C++, and although I have the idea and I have tried to write it I don't achieve how to do it.

Explanation

Imagine that I have a vector<int> that we define eveytime we run our program. Its name is mainVector This vector will have a random number of ints. And every int will have a property.

For example, we have the vector with the following values: vector<int> mainVector {1, 3, 15, 33, 35, 42, 57, 69, 73} ; and we have another vector<int> which describe the properties of every element in mainVector upon the position of the element, called properties for example: vector<int> properties {1, 1, 1, 2, 2, 2, 3, 3, 3}

What I want now, is to divide the first vector in as many smaller vectors as different properties exist. For example, in the last case, I would have three new vectors: Vector with elements with property 1: 1, 3, 15 ; vector with elements with property 2: 33, 35, 42 ; and vector with elements with property 3: 57, 69, 73 .

The problem is that I don't know how to define this, cause the first vector can be different everytime we execute our code.

Here I attached you the code with my ideas:

do
{
    for(int t=0;t<mainVector.size();t++) // id tables
    {
        string Vect("Vector");
        Vect +=t;
        vector<int> Vect

        for(int u=0;u<mainVector.size();u++)
      {
            if(properties.at(u) & t)
            {
               Vect.push_back(mainVector.at(u)); // I know this is not correct but I hope you understand what I mean
            }
        }
    }
}

Thanks in advance to all of you!!! :)

Clarification

Something important that I want to clarify: mainVector is already a subvector of another bigger vector that has been defined thanks to an imput. bigVector <int> is {1, 2, 3, 4, 5, 6, ...., 99, 100, 101, ..., n} and vector <int> properties is a vector, actually, as big a bigvector that can be different in any case, so for example, in one execution i can be {1, 1, 1, 1, 1, 1, ..., 1, 1, 2, ... 2} and ion another moment {1, 1, 1, 1, 2, 2, ..., 26, 26, 27, 49} so I think I can't do a vector of vectors as some of you are recommending, any idea??

Thanks once more!!!

You could count the number of different types in the "properties" vector and create a vector of vectors( vector<vector<int>> ). Then loop over the second vector and add the points of the first vector into the corresponding index of the new vector structure.

Something like:

bool Contains(vector<int> x, int value)
{
    bool bContains = false;
    for(int ii=0; ii<x.size(); ++ii)
    {
        if(x[ii] == value)
        {
            bContains = true;
            break;
        }
    }

    return bContains;
}

int GetIndex(vector<int> x, int value)
{
    int nIdx = -1;
    for(int ii=0; ii<x.size(); ++ii)
    {
        if(x[ii] == value)
        {
            nIdx = ii;
            break;
        }
    }

    return nIdx;
}

int main()
{
    const int SIZE=10;

    vector<int> x(SIZE);
    vector<int> y(SIZE);
    for(int ii=0; ii<SIZE; ++ii)
    {
        x[ii] = ii*SIZE+4;

        if(ii < SIZE/2)
            y[ii] = 0;
        else
            y[ii] = ii/3;
    }

    vector<int> unique(SIZE, -1);
    int nCount = 0;
    for(int ii=0; ii<y.size(); ++ii)
    {
        if(!Contains(unique, y[ii]))
            unique[nCount++] = y[ii];
    }
    unique.resize(nCount);

    vector<vector<int>> answer(nCount);
    for(int ii=0; ii<y.size(); ++ii)
        answer[GetIndex(unique, y[ii])].push_back(x[ii]);

    return 0;
}

You could use a std::map<int, std::vector<int>> to track each property and the numbers associated with that property. Eg:

typedef std::vector<int> vec_t;
typedef std::map<int, vec_t> map_t;

// the real work
map_t propMap;
for (vec_t::size_type i = 0u, i_end = mainVector.size(); i != i_end; ++i)
    propMap[properties[i]].push_back(mainVector[i]);

// printing the results
for (map_t::const_iterator miter = propMap.begin(), miter_end = propMap.end();
        miter != miter_end;
        ++miter)
{
    std::cout << "all numbers with property value of " << miter->first << ':';
    for (vec_t::const_iterator viter = miter->second.begin(), viter_end = miter->second.end();
            viter != viter_end;
            ++viter)
    {
        std::cout << ' ' << *viter;
    }
    std::cout << std::endl;
}

Prints (for the example data you've given):

all numbers with property value of 1: 1 3 15
all numbers with property value of 2: 33 35 42
all numbers with property value of 3: 57 69 73

你可以使用int的vector向量,即vector< vector< int > >

It sounds like you should either make a vector that contains a custom class or a vector of pairs

vector<pair<int, int> >

This will allow you to sort the vector without the possibility of the properties getting mismatched.

您可以设置从intvector<int>map ,其中键是属性的值,值是包含具有该属性的所有元素的子向量。

There are algorithms available already to do what you're trying to do.

If you want to make a copy of all the elements in mainVector to another vector results which satisfy a predicate, while leaving mainVector unchanged, you can use copy_if :

copy_if( mainVector.begin(), mainVector.end(), back_inserter(results), MyPredicate() );

If you want to do the same as the above except remove those items from mainVector , then you can use remove_copy_if :

mainVector.erase4( copy_if( mainVector.begin(), mainVector.end(), back_inserter(results), MyPredicate() ), mainVector.end() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM