Hi,
1)
I'm making a container for string pointers...
set<string*> strSet;
strSet.insert(...); // insert some strings
....
....
But when I come to iterate over them, I'm getting an error that there is
no operator= for iter:
set<string>::iterator iter;
for(iter = *strSet.begin(); iter != strSet.end(); iter++)
This is exactly what I did when I was learning about maps, and it worked
fine. Have I misunderstood, or should iterators work the same with all
containers?
2)
I could just as easily use a vector for these strings, and remove the
duplicates manually. However, when I tried this, nothing got removed,
even though I had no compiler errors:
vector<string> strVec;
strVec.push_back("test");
strVec.push_back("test");
strVec.push_back("test");
strVec[2].erase();
My text book states that vector is not ideal for random access removal,
but not that it is impossible. have I got this wrong?
Thanks
Steve 8 3661
* Steve Edwards: 1)
I'm making a container for string pointers...
set<string*> strSet;
strSet.insert(...); // insert some strings ... ...
But when I come to iterate over them, I'm getting an error that there is no operator= for iter:
set<string>::iterator iter; for(iter = *strSet.begin(); iter != strSet.end(); iter++)
This is exactly what I did when I was learning about maps, and it worked fine. Have I misunderstood, or should iterators work the same with all containers?
What's that '*' in there?
2) I could just as easily use a vector for these strings, and remove the duplicates manually. However, when I tried this, nothing got removed, even though I had no compiler errors:
vector<string> strVec;
strVec.push_back("test"); strVec.push_back("test"); strVec.push_back("test");
strVec[2].erase();
My text book states that vector is not ideal for random access removal, but not that it is impossible. have I got this wrong?
Both. I mean, both the textbook and you. Whether a vector is ideal for
random access removal depends on context and what's meant by removal.
However, the code you have shown calls 'erase' on one of the strings in
the vector, which changes that string but does not remove it from the
vector.
Calling 'erase' on the vector would effect a removal (in the most common
sense of the word), but in time linear in the size of the vector.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Steve Edwards wrote: Hi,
1)
I'm making a container for string pointers...
set<string*> strSet;
If possible, work with set<string> not set<string*>. Note that std::set<>
keeps its elements in a sorted order and uses that order to determine if a
new element is already in the set. For string object the order is done by
lexicographic comparison, which is very likely what you want. For object of
type string*, however, the comparison is done by comparing the *pointers*
not the pointees. strSet.insert(...); // insert some strings ... ...
But when I come to iterate over them, I'm getting an error that there is no operator= for iter:
set<string>::iterator iter;
Now, what makes you think that set<string> and set<string*> are in any way
related?
for(iter = *strSet.begin(); iter != strSet.end(); iter++)
Ask yourself what the expression *strSet.begin() will evaluate to.
This is exactly what I did when I was learning about maps, and it worked fine.
No, either it is not what you did back then, or it did not work (or both).
Have I misunderstood, or should iterators work the same with all containers?
2) I could just as easily use a vector for these strings, and remove the duplicates manually. However, when I tried this, nothing got removed, even though I had no compiler errors:
vector<string> strVec;
strVec.push_back("test"); strVec.push_back("test"); strVec.push_back("test");
strVec[2].erase();
My text book states that vector is not ideal for random access removal, but not that it is impossible. have I got this wrong?
This does not call the erase() method for the object strVec but for the
string object in strVec[2]. This is *not* what you say you intended doing.
Best
Kai-Uwe Bux
"Steve Edwards" <gf*@lineone.net> wrote in message
news:gf***********************@news.btinternet.com ...
[ 1) already answered ]
: 2)
: I could just as easily use a vector for these strings, and remove the
: duplicates manually. However, when I tried this, nothing got removed,
: even though I had no compiler errors:
:
:
: vector<string> strVec;
:
: strVec.push_back("test");
: strVec.push_back("test");
: strVec.push_back("test");
:
: strVec[2].erase();
[again, Alf already pointed out your mistake]
: My text book states that vector is not ideal for random access removal,
: but not that it is impossible. have I got this wrong?
Removal of duplicate entries can be relatively efficient if
you do it all in one go ( using <algorithm> ):
std::sort( strVec.begin(), strVec.end() );
strVec.erase( std::unique( strVec.begin(), strVec.end() )
, strVec.end()
);
In some circumstances, the performance characteristics of this
approach could compete with those of std::set.
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
In article <46************@individual.net>,
"Alf P. Steinbach" <al***@start.no> wrote: * Steve Edwards: 1)
I'm making a container for string pointers...
set<string*> strSet;
strSet.insert(...); // insert some strings ... ...
But when I come to iterate over them, I'm getting an error that there is no operator= for iter:
set<string>::iterator iter; for(iter = *strSet.begin(); iter != strSet.end(); iter++)
This is exactly what I did when I was learning about maps, and it worked fine. Have I misunderstood, or should iterators work the same with all containers? What's that '*' in there?
Ah... thanks. I was originally using pointers in my sets and got
confused.(They are subsets of a very large array of strings and I wanted
to avoid the memory overhead of having copies, but as Kai said the
sorting benefits are worth it.)
2) I could just as easily use a vector for these strings, and remove the duplicates manually. However, when I tried this, nothing got removed, even though I had no compiler errors:
vector<string> strVec;
strVec.push_back("test"); strVec.push_back("test"); strVec.push_back("test");
strVec[2].erase();
My text book states that vector is not ideal for random access removal, but not that it is impossible. have I got this wrong?
Both. I mean, both the textbook and you. Whether a vector is ideal for random access removal depends on context and what's meant by removal.
However, the code you have shown calls 'erase' on one of the strings in the vector, which changes that string but does not remove it from the vector.
Calling 'erase' on the vector would effect a removal (in the most common sense of the word), but in time linear in the size of the vector.
OK, I see what I was doing wrong, and from Ivan's example I can see how
to do it using <algorithm>. But is there a way to use erase() in the
trivial case where you want to remove a single known index? i.e.
vector<string> vec;
vec.push_back("zero");
vec.push_back("one");
vec.push_back("two");
vec.erase( [1] ); !!
In article <dt**********@news.hispeed.ch>,
"Ivan Vecerina" <IN*****************@ivan.vecerina.com> wrote: "Steve Edwards" <gf*@lineone.net> wrote in message news:gf***********************@news.btinternet.com ... [ 1) already answered ] : 2) : I could just as easily use a vector for these strings, and remove the : duplicates manually. However, when I tried this, nothing got removed, : even though I had no compiler errors: : : : vector<string> strVec; : : strVec.push_back("test"); : strVec.push_back("test"); : strVec.push_back("test"); : : strVec[2].erase(); [again, Alf already pointed out your mistake]
: My text book states that vector is not ideal for random access removal, : but not that it is impossible. have I got this wrong?
Removal of duplicate entries can be relatively efficient if you do it all in one go ( using <algorithm> ): std::sort( strVec.begin(), strVec.end() ); strVec.erase( std::unique( strVec.begin(), strVec.end() ) , strVec.end() );
In some circumstances, the performance characteristics of this approach could compete with those of std::set.
Ivan
Thanks for the code which is specific for my application.
However, I am finding it difficult to find examples (in books or on the
web) of trivial cases of dealing with specific indexing. The above kind
of method would be overkill if you know you just want to remove
strVec[7].
Steve Edwards wrote:
[snip] OK, I see what I was doing wrong, and from Ivan's example I can see how to do it using <algorithm>. But is there a way to use erase() in the trivial case where you want to remove a single known index? i.e.
vector<string> vec;
vec.push_back("zero"); vec.push_back("one"); vec.push_back("two");
vec.erase( [1] ); !!
From memory (not tested):
vec.erase( vec.begin() + 1 );
Best
Kai-Uwe Bux
In article <dt**********@murdoch.acc.Virginia.EDU>,
Kai-Uwe Bux <jk********@gmx.net> wrote: Steve Edwards wrote:
[snip] OK, I see what I was doing wrong, and from Ivan's example I can see how to do it using <algorithm>. But is there a way to use erase() in the trivial case where you want to remove a single known index? i.e.
vector<string> vec;
vec.push_back("zero"); vec.push_back("one"); vec.push_back("two");
vec.erase( [1] ); !!
From memory (not tested):
vec.erase( vec.begin() + 1 );
Best
Kai-Uwe Bux
Thanks, that appears to work as needed.
Steve
Steve Edwards wrote: In article <dt**********@news.hispeed.ch>, "Ivan Vecerina" <IN*****************@ivan.vecerina.com> wrote: std::sort( strVec.begin(), strVec.end() ); strVec.erase( std::unique( strVec.begin(), strVec.end() ) , strVec.end() ); Thanks for the code which is specific for my application. However, I am finding it difficult to find examples (in books or on the web) of trivial cases of dealing with specific indexing. The above kind of method would be overkill if you know you just want to remove strVec[7].
Yep. In that case, a solution might be
std::swap(strVec[7], strVec.back());
strVec.pop_back();
which is O(1) but of course destroys the order.
HTH,
Michiel Salters This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Karl Ebener |
last post by:
Hi!
I asked a similar question before but then changed everything to using
char-Arrays instead of the string class, but I would rather not do this
again.
So, does anyone know of a...
|
by: aquanutz |
last post by:
Ok, I have a list of strings (list<string> stringList) that I want to
sort alphabetcially, only "sort(stringList.begin(), stringList.end());
) does not work. Any insight would be helpful. Thanks!
|
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...
|
by: rsa_net_newbie |
last post by:
Hi there,
I have a Managed C++ object (in a DLL) which has a method that is
defined like ...
Generic::List<String^>^ buildList(String^ inParm)
Now, when I compile it, I get "warning C4172:...
|
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...
|
by: arnuld |
last post by:
This works fine, I welcome any views/advices/coding-practices :)
/* C++ Primer - 4/e
*
* Exercise 8.9
* STATEMENT:
* write a program to store each line from a file into a
*...
|
by: parez |
last post by:
Hi,
I am trying to serialize List<List<string>.
With the following code
public List<List<string>DataRows { get; set; }
|
by: barcaroller |
last post by:
In the boost::program_options tutorial, the author included the following
code:
cout << "Input files are: "
<< vm.as< vector<string()
<< "\n";
Basically, he is trying to print a vector...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
| |