Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using STL set and vector
02-18-2010, 08:05 AM
Post: #1
using STL set and vector
Here is a example using set so you don't accept duplicates then transferring it to a vector to be able to sort it because a set is not random access like an list.

PHP Code:
/* 
 * File:   main.cpp
 * Author: jeromescott
 *
 * Created on February 16, 2010, 11:03 AM
 */

#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

void print_results(set<intvalues); //print values with no duplicates and then print the results sorted

int main(int argcchar** argv) {
    
set<int>values;
    
int value 0;
    do
    {
        
cout<<"Enter value to add to set or enter 0 to return or a negative number to quit"<<endl;
        
cin>>value;
        if(
value 0//if user enter 0 or less stop the loop else add values
            
values.insert(value);
    } while( 
value 0);
    
print_results(values);
    return (
EXIT_SUCCESS);
   
}

void print_results(set<intvalues)
{
    
set<int>::const_iterator iread;

    
//read a list of values into a set
    
cout<<"set of values:"<<endl;
    for(
iread values.begin(); iread != values.end(); ++iread)
        
cout<<*iread<<endl;

    
//read values of a set into a vector
    
cout<<"data sorted using vector"<<endl;
    
vector<intvsorted(values.begin(),values.end());
    
partial_sort(vsorted.begin(),vsorted.end(),vsorted.end());

    
//print vector sorted
    
for(iread values.begin(); iread != values.end(); ++iread)
        
cout<<*iread<<endl;
    

Visit this user's website Find all posts by this user
Quote this message in a reply
02-18-2010, 08:09 AM
Post: #2
RE: using STL set and vector
Interesting example. You used iterators which is great !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
02-20-2010, 05:07 PM
Post: #3
RE: using STL set and vector
We have to get some C++ highlighting syntax -.-

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: