473,396 Members | 1,847 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,396 software developers and data experts.

Deleting all but the first element in a set


Hello all,

1. Are there any problems with the code below?

2. Is there a better way to accomplish what this code does?

Thanks,
Dave

#include <iostream>
#include <set>

using namespace std;

int main()
{
set<int> s;

for (int i = 0; i != 100; ++i)
s.insert(i);

// Erase all elements but the first.
s.erase(++s.begin(), s.end());

cout << s.size() << endl;
cout << *s.begin() << endl;
}

Jul 22 '05 #1
3 1512

"Dave" <be***********@yahoo.com> wrote in message
news:10*************@news.supernews.com...

Hello all,

1. Are there any problems with the code below?

Looks fine to me
2. Is there a better way to accomplish what this code does?


I don't think so.

john
Jul 22 '05 #2
I would just create a new empty set, and copy the first element from the
old one into the new one.

Chris

Dave wrote:
Hello all,

1. Are there any problems with the code below?

2. Is there a better way to accomplish what this code does?

Thanks,
Dave

#include <iostream>
#include <set>

using namespace std;

int main()
{
set<int> s;

for (int i = 0; i != 100; ++i)
s.insert(i);

// Erase all elements but the first.
s.erase(++s.begin(), s.end());

cout << s.size() << endl;
cout << *s.begin() << endl;
}


Jul 22 '05 #3
Dave wrote:

Hello all,

1. Are there any problems with the code below?

2. Is there a better way to accomplish what this code does?

Thanks,
Dave

#include <iostream>
#include <set>

using namespace std;

int main()
{
set<int> s;

for (int i = 0; i != 100; ++i)
s.insert(i);

// Erase all elements but the first.
s.erase(++s.begin(), s.end());

I cannot think of a situation in which the above would not work,
but I would avoid writing ++s.begin(), even if it were for the
sake of uniformity.

If s were a vector, ++s.begin() might or might not compile, depending
on the definition of the iterator. If the iterator were defined as a
pointer then ++s.begin() would not compile, for the reason that
non-reference return types are not lvalues.

Iterators used by class set are user-defined types (not because they
are required to be such, but because it is impossible to implement them
as pointers and satisfy all set requirements). The result of begin()
is still a non-lvalue, but you can use it with an operator function.

Denis
cout << s.size() << endl;
cout << *s.begin() << endl;
}

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: flupke | last post by:
Hi, i'm having trouble with deleting elements from a list in a for loop ============== test program ============== el = print "**** Start ****" print "List = %s " % el index = 0 for line...
20
by: Hanzo | last post by:
I'm iterating over a vector of base class pointers and deleting those which meet a certain criteria...i'm using pretty text-book code for the particular delete/erasure (it's straight out of Myers'...
12
by: Christoff Pale | last post by:
Hi, suppose I have a list of strings; I have to iterate of the strings and delete certain entries. I and not sure how to do this? for example: #include<list> #include<string>...
1
by: jez123456 | last post by:
Hi, I have a windows form with a listbox control. My code all works correctly when deleting an item from the listbox except the last item. I get the following message when trying to delete the...
3
by: Newcomsas | last post by:
Hello, I'm trying to solve a problem with JS textbox array without success. I have two buttons in my page: PLUS and MINUS; at every click on PLUS a new textbox named 'dear' is generated. So, if...
1
by: Varun Kacholia | last post by:
I apologize if there exists a standard way of deleting multiple elements from a STL hash_multiset (or even multiset for that matter) that I am unaware of. The problem, I see, with multisets is...
2
by: Muzzy | last post by:
Hi, I've used information on these newsgroups to build many pages. So I thought that now that I have my script working (something that I've been working on for about a week), I should post it so...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
One more for today.... As I add more and more lines to my RichTextBox the array that holds its strings gets bigger and bigger and the vertical scroll bar gets smaller and smaller until the...
3
by: Andy | last post by:
Hello, I have the following situation: Thread A is allocating a dataset, doing some low-level calculations and storing a pointer to the dataset in a std::list via push_back. Thread B should...
2
by: aberry | last post by:
I have strange issue when deleting element of list which I'm browsing. Here is the code , please have a look. >>> li = >>> for name in li: print name mike anil jassi >>> for name in li:
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.