Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > extracting data from std::set<string>

Reply
Thread Tools

extracting data from std::set<string>

 
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      10-19-2012
I am trying to extract data from a set container, but to no avail. This is what I have done.
int main(){
std::set<const std::string> data;
data.insert("one");
data.insert("two");
data.insert("three");
int counter = 0;

std::set<std::string>::iterator it;
for (it = data.begin(); it != data.end(); it++) {
counter++;
std::cout << *it;
}
return 0;
}
 
Reply With Quote
 
 
 
 
Rui Maciel
Guest
Posts: n/a
 
      10-19-2012
Rene Ivon Shamberger wrote:

> I am trying to extract data from a set container, but to no avail. This is
> what I have done. int main(){
> std::set<const std::string> data;
> data.insert("one");
> data.insert("two");
> data.insert("three");
> int counter = 0;
>
> std::set<std::string>::iterator it;
> for (it = data.begin(); it != data.end(); it++) {
> counter++;
> std::cout << *it;
> }
> return 0;
> }



here's your problem:

> std::set<const std::string> data;


The STL allocates objects using C++'s allocators, which only store non-
const, non-reference object types. You are trying to allocate a const-
object type. Hence, the flood of compiler errors you should be getting from
this.

Replace the above with:

std::set<std::string> data;

and everything will work fine.

While you are at it, use const iterators to enforce the object's const-ness.
So, replace:


> std::set<std::string>::iterator it;


with


std::set<std::string>::const_iterator it;


Hope this helps,
Rui Maciel
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      10-19-2012
Am Freitag, 19. Oktober 2012 16:35:45 UTC+2 schrieb Rene Ivon Shamberger:
> I am trying to extract data from a set container, but to no avail.
> This is what I have done.
>
> int main(){
> std::set<const std::string> data;


I see a 'const'.

[snip]

> std::set<std::string>::iterator it;
> for (it = data.begin(); it != data.end(); it++) {


I don't see a 'const' here.

Just lose 'const' in the definition of 'data'.

PS.: Next time think about including a description of what exactly does not work as expected, possibly including compiler error messages.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting data from a DataSet =?Utf-8?B?Z2xlbm4=?= ASP .Net 0 03-21-2006 06:39 PM
Extracting Data from Forms Roedy Green Java 2 08-25-2005 11:41 PM
Extracting Tables and columns with data types from a database RSH ASP .Net 1 06-02-2005 02:43 PM
Extracting text data from MS Word document Max Java 6 09-16-2004 11:01 PM
Extracting ASP.NET data in another Class walterd ASP .Net 1 04-28-2004 01:32 AM



Advertisments