Reputation: 161
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!!!
Upvotes: 1
Views: 1624
Reputation: 3049
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;
}
Upvotes: 2
Reputation: 62975
You could use a std::map<int, std::vector<int>>
to track each property and the numbers associated with that property. E.g.:
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
Upvotes: 2
Reputation: 101456
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() );
Upvotes: 0
Reputation: 500357
You could set up a map
from int
to vector<int>
, where the key is the value of the property and the value is the sub-vector containing all elements with that property.
Upvotes: 0
Reputation: 15664
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.
Upvotes: 0
Reputation: 44503
You can use a vector of vector of int, that is vector< vector< int > >
.
Upvotes: 0