I have defined a dynamic memory as * indexs (vector of vector) to store set of values. Then, I appended this memory. Afterthat, I want to get a print of these values. For that I am using another function as follows. For that function, I am calling above values by references. I don’t know proper way to use iterator to print those values.
Here is my piece of codes and the error I got for that.
vector< vector<unsigned int> >* indexs = new vector< vector<unsigned int> >(9);
for( ){ //finish data adding into indexs }
calcParameter(*indexs); //call function
void myclass::calcParameter(vector< vector<unsigned int> >const &indexs){
vector< vector<unsigned int> > :: iterator cell_i;
for (cell_i=indexs->begin(); cell_i != indexs->end();cell_i++){ //this is line 305 in my program
vector<unsigned int> :: iterator pij;
for (pij=cell_i->begin(); pij =! cell_i->end(); pij++){
cout<<" "<<*pij;
}
cout<<endl;
}
}
error message: 305-base operand of `->' has non-pointer type `const
std::vector<std::vector<unsigned int, std::allocator<unsigned int> >,
std::allocator<std::vector<unsigned int, std::allocator<unsigned int> > > >'
when I use simple for loop to get that, It works but I want to learn how to use iterator for this.
void myclass::calcParameter(vector< vector<unsigned int> >const &indexs){
for (int i=0; i<indexs.size(); i++){
for (int j=0; j< indexs[i].size(); j++){
cout<<" "<<indexs[i][j];
}
cout<<endl;
}
}
Any help please..
indexs is not a pointer, it's a reference, that's all. You need to say
indexs.begin()
and not
indexs->begin()
(etc).
Related
Hi I am trying to convert an iterator into an int. The iterator is given by using std::find however I am getting the error: no viable overloaded '='. I am thinking the issue may be because the two vectors I am using are vectors of pairs but I am really not sure. I got this error also:
candidate function (the implicit copy assignment operator) not viable: no known conversion from '__normal_iterator<std::pair<int, int> *, vector<std::pair<int, int>, allocator<std::pair<int, int>>>>' to
'const __normal_iterator<int *, vector<int, allocator<int>>>' for 1st argument
class __normal_iterator
Here is that segment of my code:
std::vector<std::pair<int, int>> nodat;
std::vector<std::pair<int, int>> unknown;
std::vector<std::pair<int, int>> measure;
for(int i = 0; i<vec.size(); i++){
if(std::find(iso.begin(), iso.end(), vec[i]) == iso.end()){
nodat.push_back(vec[i]);
}
else{
vector<int>::iterator it;
it = std::find(iso.begin(), iso.end(), vec[i]);
int index = std::distance(iso.begin(), it);
if(HalfLife[index] == 1e-50){
unknown.push_back(vec[i]);
}
else if(HalfLife[index] > 1e-8){
measure.push_back(vec[i]);
}
}
}
In a certain competitive coding, i need to use a data-structure mentioned above but it gave me an error, why it threw an error and which is the simplest data-structure i can use for this
A vector only contains one type. You are trying to put two into the vector. You could instead do vector<pair<int, pair<int, int>> and that would be fine.
We can not declare vector as vector < int , pair < int , int > > Because Vector is a sequence of contiguous storage of elements of same type .
And this represents the associative container having key value property like mapping .
so We cant declare vector as vector< int , pair < int ,int > > .
We can declare it as vector< pair < int , pair < int , int > > or vector< data_type>
My program has a nested std::map object like
map < int, set < void *> *> m;
My program coredown in some situation, and I don't know the reason.
I suspect the problem is in the nested map.
I only know using pmap to print simple map objects like std::map<int, int>,
but don't know how to print more complicated map objects.
If a map is defined as map< int, int > m;, then
gdb pmap m int int
does work, but if a map is defined as
map< int, set < void * > * > m;
then
gdb pmap m int set < void *> *
does not work
std::vector<std::vector< std::pair<int, int> > > offset_table;
for (int i = 0; i < (offset.Width()*offset.Width()); ++i)
{
offset_table.push_back( std::vector< std::pair<int, int> > );
}
This is my code, but I am getting the errors:
main.cpp: In function ‘void Compress(const Image<Color>&, Image<bool>&, Image<Color>&, Image<Offset>&)’:
main.cpp:48:66: error: expected primary-expression before ‘)’ token
I do not want any values in the pairs, I just would like to have a vector of empty vectors at the moment. How would I do this?
You want to construct a vector to pass to push_back and you're just missing parentheses:
offset_table.push_back( std::vector< std::pair<int, int> >() );
Or, instead of your loop, you could just do the following. It's better because the vector will allocate just the right amount of memory in a single allocation:
offset_table.resize( offset.Width()*offset.Width(), std::vector< std::pair<int, int> >() );
Or this, which is more concise because it's using resize's default 2nd argument:
offset_table.resize( offset.Width()*offset.Width() );
std::vector<std::vector< std::pair<int, int> > > offset_table;
This is is a 2d array so you need to use nested array. For only getting the length if inner vector.
for(vector< pair<int, int >> vv in offset_table)
{
if(vv.size() == 0)
{
// this is your target.
}
}
I would like to search within a vector<std::pair<int, vector<int> > >. This won't work due to the vector parameters:
std::vector<std::pair<int, std::vector<int> > > myVec;
iterator = find(myVec.begin(), myVec.end(), i);
Search would be on the first std::pair template parameter (int), in order to get the vector associated with it.
std::vector<std::pair<int, std::vector<int> > > myVec;
This requires C++0x for the lambda expression:
typedef std::pair<int, std::vector<int>> pair_type
std::find_if(myVec.begin(), myVec.end(), [i](pair_type const& pair)
{ return pair.first == i; });
If you're not using C++0x then either roll out your own loop or use something like Boost.Phoenix/Boost.Lambda.
Or, for both cases, why not use std::map?
You could make do with the following (pretty ugly) functoid:
struct FindFirst {
FindFirst(int i) : toFind(i) { }
int toFind;
bool operator()
( const std::pair<int, std::vector<int> > &p ) {
return p.first==toFind;
}
};
using it like this ( I couldn't get bind2nd to work - that's why I used the c'tor ):
int valueToFind = 4;
std::find_if(myVec.begin(), myVec.end(), FindFirst(valueToFind));
I think what you would like is a map:
std::map< int, vector< int > > foo;
You can then add elements, search by key etc:
int key = 4; //This will be the key
vector<int> value(5, 4); //Fill some values (5 4's in this case) into the vector
foo[key]=value; //Adds the pair to the map. Alternatively;
foo.insert( make_pair(key, value) ); //Does the same thing (in this context)
Looking at the way you've done things though, you might be wanting a std::multimap (which allows multiple values to have the same key) Class docs here
You're trying to map an int to a vector of int.
So try map<int, vector<int> >.
The second template parameter of a vector is the allocator - your compiler can probably puzzle out what you wanted to say, the declaration is wrong anyway. What you probably want is some sort of map type, like iammilind suggested.