?
Solved

Generating a combination of strings

Posted on 2004-11-07
3
Medium Priority
?
178 Views
Last Modified: 2010-04-01

I am looking for opitmal method of generating pair of numbers from string number.
For example string 12345
Should produce: 11 12 13 14 15 22 23 24 25 33 34 35 44 45 55
  or
String 121256708
Should produce: 11 12 15 16 17 10 18 22 21 25 26 27 20 28 55 56 57 50 58 66 67 60 68 77 70 78 00 08 88

I've perused the STL algorithms but I'm coming up short on the right combination - if you will.  
Idea.

Thanks in advance
0
Comment
Question by:forums_mp
  • 2
3 Comments
 
LVL 1

Assisted Solution

by:gugario
gugario earned 140 total points
ID: 12519040
Hey, try this:

#include <iostream>
#include <string>
#include <deque>
using namespace std;

int main()
{
        string model;
        cout << "enter string: ";                                                      
         cin >> model;
        deque<string> pairs;                                                            
        string a;
        for (int i=0; i< model.size(); i++)
                for (int j=i; j< model.size(); j++)
                {
                        a = "";
                        a+=model[i];
                        a+=model[j];
                        pairs.push_back(a);
                }

        for (int i=0; i< pairs.size(); i++)
                cout << pairs[i] << " ";                                                
        cout << endl;
        return 1;
}
0
 

Author Comment

by:forums_mp
ID: 12519585

Your approach doesnt remove duplicates, nonetheless, I had something similar.  I thought perhaps there was a more elegant approach in the form of STL algorithms.  
0
 
LVL 1

Accepted Solution

by:
gugario earned 140 total points
ID: 12519734
well, if you want to remove duplicates, an easy way to do it would be to use a <set> instead of a <deque> ... A set is a collection of unique variables, so if you try to insert something that already exists, it won't get inserted twice...

the only difference in the code would be that you would need an iterator to go through the set

set<string> pairs;

... code to load up the set ...

for ( set<string>::iterator t = pairs.begin(), t!= pairs.end(); t++)
{
      cout << *t << " ";
     cout << endl;
    return1;
}

Now, if you consider "21" and "12" a duplicate pair, and you want to watch out for that as well, what you could do is sort the string before you insert it into the set... this can be accomplished like this

#include <algorithm>

sort ( a );
pairs.push_back(a);

hope this helps,

Gustavo.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question