473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
7 5730
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vinu | last post by:
Hi I am facing a problem in compilation the error is like this In constructor xServices::CServices<TImp>::StHoldClientList::StHoldClientList(std::set<TImp*, std::less<TImp*>,...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
8
by: Steve Edwards | last post by:
Hi, 1) I'm making a container for string pointers... set<string*> strSet; strSet.insert(...); // insert some strings ....
5
by: KL | last post by:
I have a problem trying to write to a binary file that I want to name after a particular name from a set. My code for the function follows, please excuse the horrible mistakes you may see, I am a...
4
by: Stuart Moore | last post by:
Hi, I'm quite new to templates and I seem to be getting myself messed up. I want to write a function that takes a map<T, int> and a set<T>, iterates over the set, and increments the corresponding...
4
by: lutorm | last post by:
Hi all, I'm having a problem writing template functions that take vector<T>::iterator as arguments and I'm sure you guys can set me straight. Like this: #include<vector> using namespace std; ...
6
by: Martin | last post by:
Hi I need to maintain a <setof pointers to objects, and it must be sorted based on the values of pointed objects, not pointer values. I can achieve this easily by defining my own comparing...
9
by: Zootal | last post by:
The following code will compile. I have one line commented out because it won't compile. Why can I not do this: //set<string>::iterator iterator = s->begin(); Why do I have to deference the...
22
by: SETT Programming Contest | last post by:
The SETT Programming Contest: The fastest set<Timplementation Write the fastest set<Timplementation using only standard C++/C. Ideally it should have the same interface like std::set. At least...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.