c++ - Dividing a vector into smaller vectors upon its properties -


i want write in c++, , although have idea , have tried write don't achieve how it.

explanation

imagine have vector<int> define eveytime run our program. name mainvector vector have random number of ints. , every int have property.

for example, have vector following values: vector<int> mainvector {1, 3, 15, 33, 35, 42, 57, 69, 73}; , have vector<int> describe properties of every element in mainvector upon position of element, called properties example: vector<int> properties {1, 1, 1, 2, 2, 2, 3, 3, 3}

what want now, divide first vector in many smaller vectors different properties exist. example, in last case, have 3 new vectors: vector elements property 1: 1, 3, 15; vector elements property 2: 33, 35, 42; , vector elements property 3: 57, 69, 73.

the problem don't know how define this, cause first vector can different everytime execute our code.

here attached code 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)); // know not correct hope understand mean             }         }     } } 

thanks in advance of you!!! :)

clarification

something important want clarify: mainvector subvector of bigger vector has been defined imput. bigvector <int> {1, 2, 3, 4, 5, 6, ...., 99, 100, 101, ..., n} , vector <int> properties vector, actually, big bigvector can different in case, example, in 1 execution can {1, 1, 1, 1, 1, 1, ..., 1, 1, 2, ... 2} , ion moment {1, 1, 1, 1, 2, 2, ..., 26, 26, 27, 49} think can't vector of vectors of recommending, idea??

thanks once more!!!

you use std::map<int, std::vector<int>> track each property , numbers associated property. e.g.:

typedef std::vector<int> vec_t; typedef std::map<int, vec_t> map_t;  // real work map_t propmap; (vec_t::size_type = 0u, i_end = mainvector.size(); != i_end; ++i)     propmap[properties[i]].push_back(mainvector[i]);  // printing results (map_t::const_iterator miter = propmap.begin(), miter_end = propmap.end();         miter != miter_end;         ++miter) {     std::cout << "all numbers property value of " << miter->first << ':';     (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 example data you've given):

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


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -