By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,017 Members | 997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

A question about the std::set<>::iterator

P: n/a
I have a problem about the std::set<>iterator. After finding a term in
the std::set<>, i want to know the distance from
the current term to the begin(). But i have got a error. Please offer
me help, thank you. I am a freshman about the STL. The following is
the code.
#include <set>
#include <iostream>
#include <vector>
int main()
{
std::set<unsigned inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsigned intb;
b.insert(6); b.insert(7); b.insert(8); b.insert(9); b.insert(10);
std::vector<std::set<unsigned int ab(2);
ab[0]= a; ab[1]=b;
{
const unsigned int i=0, j=5;
std::set<unsigned int>::iterator it=ab[i].find(5);
if(it!=ab[i].end()) std::cout<<it-ab[i].begin()<<std::endl;
else { std::cout<"There is no:"<<j<<" in "<<i<<std::endl;
}
}
return 0;
}

This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_Tp,
_Alloc>::operator[] [with _Tp = std::set<unsigned int,
std::less<unsigned int>, std::allocator<unsigned int, _Alloc =
std::allocator<std::set<unsigned int, std::less<unsigned int>,
std::allocator<unsigned int >](0u))->std::set<_Key, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsigned int>, _Alloc = std::allocator<unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:180: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)

Oct 3 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Renzr wrote:
I have a problem about the std::set<>iterator. After finding a term in
the std::set<>, i want to know the distance from
the current term to the begin(). But i have got a error. Please offer
me help, thank you. I am a freshman about the STL. The following is
the code.
#include <set>
#include <iostream>
#include <vector>
int main()
{
std::set<unsigned inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsigned intb;
b.insert(6); b.insert(7); b.insert(8); b.insert(9); b.insert(10);
std::vector<std::set<unsigned int ab(2);
ab[0]= a; ab[1]=b;
{
const unsigned int i=0, j=5;
std::set<unsigned int>::iterator it=ab[i].find(5);
if(it!=ab[i].end()) std::cout<<it-ab[i].begin()<<std::endl;
There is no operator - defined for it, that's what the compiler is
telling you. If you need to know how far it's from the start (though
I am not sure why you need that), use 'std::distance' function.
else { std::cout<"There is no:"<<j<<" in "<<i<<std::endl;
}
}
return 0;
}

This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_Tp,
_Alloc>::operator[] [with _Tp = std::set<unsigned int,
std::less<unsigned int>, std::allocator<unsigned int, _Alloc =
std::allocator<std::set<unsigned int, std::less<unsigned int>,
std::allocator<unsigned int >](0u))->std::set<_Key, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsigned int>, _Alloc = std::allocator<unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:180: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '07 #2

P: n/a
The problem here is that there is no operator- for the iterator.

Take a look at the distance algorithm.

On Oct 3, 2:52 pm, Renzr <renzhengy...@gmail.comwrote:
I have a problem about the std::set<>iterator. After finding a term in
the std::set<>, i want to know the distance from
the current term to the begin(). But i have got a error. Please offer
me help, thank you. I am a freshman about the STL. The following is
the code.
#include <set>
#include <iostream>
#include <vector>
int main()
{
std::set<unsigned inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsigned intb;
b.insert(6); b.insert(7); b.insert(8); b.insert(9); b.insert(10);
std::vector<std::set<unsigned int ab(2);
ab[0]= a; ab[1]=b;
{
const unsigned int i=0, j=5;
std::set<unsigned int>::iterator it=ab[i].find(5);
if(it!=ab[i].end()) std::cout<<it-ab[i].begin()<<std::endl;
else { std::cout<"There is no:"<<j<<" in "<<i<<std::endl;
}
}
return 0;

}

This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_Tp,
_Alloc>::operator[] [with _Tp = std::set<unsigned int,
std::less<unsigned int>, std::allocator<unsigned int, _Alloc =
std::allocator<std::set<unsigned int, std::less<unsigned int>,
std::allocator<unsigned int >](0u))->std::set<_Key, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsigned int>, _Alloc = std::allocator<unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:180: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)

Oct 3 '07 #3

P: n/a
On Oct 3, 9:56 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Renzr wrote:
I have a problem about the std::set<>iterator. After finding a term in
the std::set<>, i want to know the distance from
the current term to the begin(). But i have got a error. Please offer
me help, thank you. I am a freshman about the STL. The following is
the code.
#include <set>
#include <iostream>
#include <vector>
int main()
{
std::set<unsigned inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsigned intb;
b.insert(6); b.insert(7); b.insert(8); b.insert(9); b.insert(10);
std::vector<std::set<unsigned int ab(2);
ab[0]= a; ab[1]=b;
{
const unsigned int i=0, j=5;
std::set<unsigned int>::iterator it=ab[i].find(5);
if(it!=ab[i].end()) std::cout<<it-ab[i].begin()<<std::endl;

There is no operator - defined for it, that's what the compiler is
telling you. If you need to know how far it's from the start (though
I am not sure why you need that), use 'std::distance' function.
else { std::cout<"There is no:"<<j<<" in "<<i<<std::endl;
}
}
return 0;
}
This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_Tp,
_Alloc>::operator[] [with _Tp = std::set<unsigned int,
std::less<unsigned int>, std::allocator<unsigned int, _Alloc =
std::allocator<std::set<unsigned int, std::less<unsigned int>,
std::allocator<unsigned int >](0u))->std::set<_Key, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsigned int>, _Alloc = std::allocator<unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:180: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Thanks for your solution.
The resean is in my FEM code, I want to get the local position of DOF
in the ith row.
So i design the std::vector<std::set<unsigned int.
Maybe std::vector<std::vector<unsigned int is prefered.
In my loop to ith row (std::vector[i]), several "unsigned int" will be
added into std::vector[i].
And i need the sorted "unsigned int" in std::vector[i]. So, I have two
ways.
The first is to design the std::vector[i] as std::vector<unsigned
int>. But this way we should involve in std::sort() on
std::vector<unsigned int>. The second way is to design the
std::vector[i] as std::set<unsigned int>.
In the second way, when I need get the [j]th value in std::vector[i]--
>std::set<>, it is not easy like std::vector<>.
And also, to get the local postion of value T in std::set<unsigned
intof std::vector[i] is also not easy like std::vector<>.
That is my problem.
So, I have two questions.
1st, Why not std::set<offer operator [i], where i is the local
index?
2nd, Why not std::set<>::iterator is invalid in "iterator- begin()"?

Oct 3 '07 #4

P: n/a
Renzr wrote:
[..] I have two questions.
1st, Why not std::set<offer operator [i], where i is the local
index?
Because 'std::set' is not a sequential container. Only sequences
are required to provide indexing.
2nd, Why not std::set<>::iterator is invalid in "iterator- begin()"?
Because it's not a random-access iterator. It's a bidirectional
iterator that is not required to provide the subtraction operator.

You need to decide on what container to use based on which part of
its functionality you use the most. If it's insertion and iteration,
and rarely getting the "index", 'std::set' would do nicely. If you
insert rarely (or only once), and then you need to index within the
container the rest of its life, you might be better off with the
vector, as you pointed out. Yes, you need it sorted, but that's
not as expensive as you might think. If you know the maximum # of
DOF, you can reserve a certain size in your vectors to prevent
reallocations when inserting.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '07 #5

P: n/a
On 2007-10-03 16:12, Renzr wrote:
On Oct 3, 9:56 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Renzr wrote:
I have a problem about the std::set<>iterator. After finding a term in
the std::set<>, i want to know the distance from
the current term to the begin(). But i have got a error. Please offer
me help, thank you. I am a freshman about the STL. The following is
the code.
#include <set>
#include <iostream>
#include <vector>
int main()
{
std::set<unsigned inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsigned intb;
b.insert(6); b.insert(7); b.insert(8); b.insert(9); b.insert(10);
std::vector<std::set<unsigned int ab(2);
ab[0]= a; ab[1]=b;
{
const unsigned int i=0, j=5;
std::set<unsigned int>::iterator it=ab[i].find(5);
if(it!=ab[i].end()) std::cout<<it-ab[i].begin()<<std::endl;

There is no operator - defined for it, that's what the compiler is
telling you. If you need to know how far it's from the start (though
I am not sure why you need that), use 'std::distance' function.
else { std::cout<"There is no:"<<j<<" in "<<i<<std::endl;
}
}
return 0;
}
This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_Tp,
_Alloc>::operator[] [with _Tp = std::set<unsigned int,
std::less<unsigned int>, std::allocator<unsigned int, _Alloc =
std::allocator<std::set<unsigned int, std::less<unsigned int>,
std::allocator<unsigned int >](0u))->std::set<_Key, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsigned int>, _Alloc = std::allocator<unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:180: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Thanks for your solution.
The resean is in my FEM code, I want to get the local position of DOF
in the ith row.
So i design the std::vector<std::set<unsigned int.
Maybe std::vector<std::vector<unsigned int is prefered.
In my loop to ith row (std::vector[i]), several "unsigned int" will be
added into std::vector[i].
And i need the sorted "unsigned int" in std::vector[i]. So, I have two
ways.
The first is to design the std::vector[i] as std::vector<unsigned
int>. But this way we should involve in std::sort() on
std::vector<unsigned int>. The second way is to design the
std::vector[i] as std::set<unsigned int>.
Actually, it is probably cheaper to insert the elements in the vector
and then sort it that it is to insert them into the set. For large
number of elements the difference can be quite noticeable.

--
Erik Wikström
Oct 3 '07 #6

P: n/a
On Oct 3, 10:21 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Renzr wrote:
[..] I have two questions.
1st, Why not std::set<offer operator [i], where i is the local
index?

Because 'std::set' is not a sequential container. Only sequences
are required to provide indexing.
2nd, Why not std::set<>::iterator is invalid in "iterator- begin()"?

Because it's not a random-access iterator. It's a bidirectional
iterator that is not required to provide the subtraction operator.

You need to decide on what container to use based on which part of
its functionality you use the most. If it's insertion and iteration,
and rarely getting the "index", 'std::set' would do nicely. If you
insert rarely (or only once), and then you need to index within the
container the rest of its life, you might be better off with the
vector, as you pointed out. Yes, you need it sorted, but that's
not as expensive as you might think. If you know the maximum # of
DOF, you can reserve a certain size in your vectors to prevent
reallocations when inserting.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks for your solution again. I think I have got you.
In my code, the std::vector<std::set<unsigned int is adopted as the
sparse pattenr of the finite-element mesh.
So to the ith row, its relative DOFs are only inserted once, and it
will be used in the assemble frequently.
Yes, I know the maximun # of DOF. And from you, I will chose the
std::vector<>::reserce() to pre-allocate space.
Your solution is perfect.
I will adopte the std::vector<std::vector<unsigned int.
And the std::sort(std::vector<>:iterator, std::vector<>::iteraotr) is
not expensive only more.
:) Thanks.
Best regards,
Ren ZhengYong

Oct 3 '07 #7

P: n/a
On Oct 3, 10:35 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-10-03 16:12, Renzr wrote:
On Oct 3, 9:56 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Renzr wrote:
I have a problem about the std::set<>iterator. After finding a term in
the std::set<>, i want to know the distance from
the current term to the begin(). But i have got a error. Please offer
me help, thank you. I am a freshman about the STL. The following is
the code.
#include <set>
#include <iostream>
#include <vector>
int main()
{
std::set<unsigned inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsigned intb;
b.insert(6); b.insert(7); b.insert(8); b.insert(9); b.insert(10);
std::vector<std::set<unsigned int ab(2);
ab[0]= a; ab[1]=b;
{
const unsigned int i=0, j=5;
std::set<unsigned int>::iterator it=ab[i].find(5);
if(it!=ab[i].end()) std::cout<<it-ab[i].begin()<<std::endl;
There is no operator - defined for it, that's what the compiler is
telling you. If you need to know how far it's from the start (though
I am not sure why you need that), use 'std::distance' function.
else { std::cout<"There is no:"<<j<<" in "<<i<<std::endl;
}
}
return 0;
}
This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_Tp,
_Alloc>::operator[] [with _Tp = std::set<unsigned int,
std::less<unsigned int>, std::allocator<unsigned int, _Alloc =
std::allocator<std::set<unsigned int, std::less<unsigned int>,
std::allocator<unsigned int >](0u))->std::set<_Key, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsigned int>, _Alloc = std::allocator<unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:180: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks for your solution.
The resean is in my FEM code, I want to get the local position of DOF
in the ith row.
So i design the std::vector<std::set<unsigned int.
Maybe std::vector<std::vector<unsigned int is prefered.
In my loop to ith row (std::vector[i]), several "unsigned int" will be
added into std::vector[i].
And i need the sorted "unsigned int" in std::vector[i]. So, I have two
ways.
The first is to design the std::vector[i] as std::vector<unsigned
int>. But this way we should involve in std::sort() on
std::vector<unsigned int>. The second way is to design the
std::vector[i] as std::set<unsigned int>.

Actually, it is probably cheaper to insert the elements in the vector
and then sort it that it is to insert them into the set. For large
number of elements the difference can be quite noticeable.

--
Erik Wikström
Thanks for your solution.
Your help offer me a right choice on the std::vector<and std::set<>

Oct 3 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.