473,651 Members | 2,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about the std::set<>::ite rator

I have a problem about the std::set<>itera tor. 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<unsign ed inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsign ed 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<unsign ed int>::iterator it=ab[i].find(5);
if(it!=ab[i].end()) std::cout<<it-ab[i].begin()<<std:: endl;
else { std::cout<"Ther e is no:"<<j<<" in "<<i<<std::endl ;
}
}
return 0;
}

This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_T p,
_Alloc>::operat or[] [with _Tp = std::set<unsign ed int,
std::less<unsig ned int>, std::allocator< unsigned int, _Alloc =
std::allocator< std::set<unsign ed int, std::less<unsig ned int>,
std::allocator< unsigned int >](0u))->std::set<_Ke y, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsig ned int>, _Alloc = std::allocator< unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:1 80: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_itera tor_base&, const std::_Bit_itera tor_base&)

Oct 3 '07 #1
7 5762
Renzr wrote:
I have a problem about the std::set<>itera tor. 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<unsign ed inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsign ed 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<unsign ed 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<"Ther e is no:"<<j<<" in "<<i<<std::endl ;
}
}
return 0;
}

This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_T p,
_Alloc>::operat or[] [with _Tp = std::set<unsign ed int,
std::less<unsig ned int>, std::allocator< unsigned int, _Alloc =
std::allocator< std::set<unsign ed int, std::less<unsig ned int>,
std::allocator< unsigned int >](0u))->std::set<_Ke y, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsig ned int>, _Alloc = std::allocator< unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:1 80: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_itera tor_base&, const std::_Bit_itera tor_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...@g mail.comwrote:
I have a problem about the std::set<>itera tor. 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<unsign ed inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsign ed 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<unsign ed int>::iterator it=ab[i].find(5);
if(it!=ab[i].end()) std::cout<<it-ab[i].begin()<<std:: endl;
else { std::cout<"Ther e is no:"<<j<<" in "<<i<<std::endl ;
}
}
return 0;

}

This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_T p,
_Alloc>::operat or[] [with _Tp = std::set<unsign ed int,
std::less<unsig ned int>, std::allocator< unsigned int, _Alloc =
std::allocator< std::set<unsign ed int, std::less<unsig ned int>,
std::allocator< unsigned int >](0u))->std::set<_Ke y, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsig ned int>, _Alloc = std::allocator< unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:1 80: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_itera tor_base&, const std::_Bit_itera tor_base&)

Oct 3 '07 #3
On Oct 3, 9:56 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Renzr wrote:
I have a problem about the std::set<>itera tor. 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<unsign ed inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsign ed 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<unsign ed 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<"Ther e is no:"<<j<<" in "<<i<<std::endl ;
}
}
return 0;
}
This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_T p,
_Alloc>::operat or[] [with _Tp = std::set<unsign ed int,
std::less<unsig ned int>, std::allocator< unsigned int, _Alloc =
std::allocator< std::set<unsign ed int, std::less<unsig ned int>,
std::allocator< unsigned int >](0u))->std::set<_Ke y, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsig ned int>, _Alloc = std::allocator< unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:1 80: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_itera tor_base&, const std::_Bit_itera tor_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<unsign ed 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<uns igned
int>. But this way we should involve in std::sort() on
std::vector<uns igned int>. The second way is to design the
std::vector[i] as std::set<unsign ed 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<unsign ed
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<>::ite rator 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<>::ite rator 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...@com Acast.netwrote:
>Renzr wrote:
I have a problem about the std::set<>itera tor. 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<unsign ed inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsign ed 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<unsign ed 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<"Ther e is no:"<<j<<" in "<<i<<std::endl ;
}
}
return 0;
}
This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_T p,
_Alloc>::operat or[] [with _Tp = std::set<unsign ed int,
std::less<unsig ned int>, std::allocator< unsigned int, _Alloc =
std::allocator< std::set<unsign ed int, std::less<unsig ned int>,
std::allocator< unsigned int >](0u))->std::set<_Ke y, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsig ned int>, _Alloc = std::allocator< unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:1 80: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_itera tor_base&, const std::_Bit_itera tor_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<unsign ed 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<uns igned
int>. But this way we should involve in std::sort() on
std::vector<uns igned int>. The second way is to design the
std::vector[i] as std::set<unsign ed 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...@com Acast.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<>::ite rator 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<unsign ed int.
And the std::sort(std:: vector<>:iterat or, 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...@com Acast.netwrote:
Renzr wrote:
I have a problem about the std::set<>itera tor. 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<unsign ed inta;
a.insert(1); a.insert(2); a.insert(3); a.insert(4); a.insert(5);
std::set<unsign ed 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<unsign ed 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<"Ther e is no:"<<j<<" in "<<i<<std::endl ;
}
}
return 0;
}
This is the error:
error: no match for 'operator-' in 'it - (+(&ab)->std::vector<_T p,
_Alloc>::operat or[] [with _Tp = std::set<unsign ed int,
std::less<unsig ned int>, std::allocator< unsigned int, _Alloc =
std::allocator< std::set<unsign ed int, std::less<unsig ned int>,
std::allocator< unsigned int >](0u))->std::set<_Ke y, _Compare,
_Alloc>::begin [with _Key = unsigned int, _Compare =
std::less<unsig ned int>, _Alloc = std::allocator< unsigned int>]()'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/
bits/stl_bvector.h:1 80: note: candidates are: ptrdiff_t std::operator-
(const std::_Bit_itera tor_base&, const std::_Bit_itera tor_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<unsign ed 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<uns igned
int>. But this way we should involve in std::sort() on
std::vector<uns igned int>. The second way is to design the
std::vector[i] as std::set<unsign ed 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
1834
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*>, std::allocator<TImp*> >&)': : error: expected `;' before "pos" : error: `pos' undeclared (first use this function) : error: (Each undeclared identifier is reported only once for each function it appears in.)
4
2745
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
3672
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
3101
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 student, and trying my best. void retrieveImage(set<string>finalset, string hostname, string filename){ set<string>::iterator i; string img, filetoget; char test; for (i = finalset.begin();i != finalset.end();i++){
4
2635
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 int in the map (or sets it to 1 if it doesn't already exist). I can write the function itself for a given type, but I can't figure out what template statement(s) I need to put before it to make it generalise. Stuff I've tried unsuccessfully is...
4
2590
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; template<typename T> void test2(typename std::vector<T>::iterator b)
6
4738
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 predicate for the <set>. Here is an example: #include <string> #include <set> using namespace std;
9
2688
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 set when I receive it as a pointer? Is that just the way we are supposed to do it, or am I missing something? -------------------------------------------------
22
2685
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 the following methods must be implemented: insert(), find(), begin(), end(), erase(), size(), operator<(), and at least the forward iterator. Here, speed and correctness are the 2 most important factors. Functionally it should behave similar to...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8792
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.