Using a map against defined input

closed account (GL1Rko23)
What I want to do is have a defined string as the input and use that to run it against the keys, so I can pull out the values. I have some functions beneath for searching, a possibility would be to parse the input and use something along those lines.

Any suggestions or things to make this easier? I would like to try and use a map for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <map>
#include <string>



int main()
{
    //std::string word;
    //word = "1110001010101000100000001110111010111000101011100010100011101011101000111010111";
    
 
    
    typedef std::map <std::string, char> morse_charT;
    morse_charT my_morse;
    my_morse["000"] = ' ';
    my_morse["10111"] = 'a';
    my_morse["111010101"] = 'b';
    my_morse["11101011101"] = 'c';
    my_morse["1110101"] = 'd';
    my_morse["1"] = 'e';
    my_morse["101011101"] = 'f';
    my_morse["111011101"] = 'g';
    my_morse["1010101"] = 'h';
    my_morse["101"] = 'i';
    my_morse["1011101110111"] = 'j';
    my_morse["111010111"] = 'k';
    my_morse["101110101"] = 'l';
    my_morse["1110111"] = 'm';
    my_morse["11101"] = 'n';
    my_morse["11101110111"] = 'o';
    my_morse["10111011101"] = 'p';
    my_morse["1110111010111"] = 'q';
    my_morse["1011101"] = 'r';
    my_morse["10101"] = 's';
    my_morse["111"] = 't';
    my_morse["1010111"] = 'u';
    my_morse["101010111"] = 'v';
    my_morse["1110111"] = 'w';
    my_morse["11101010111"] = 'x';
    my_morse["1110101110111"] = 'y';
    my_morse["11101110101"] = 'z';
    
    
    morse_charT::iterator  it= my_morse.find("111");
    if( it != my_morse.end() ) std::cout /*<< "First: "*/ << it->second;
    
    it= my_morse.find("1010101");
    if( it != my_morse.end() ) std::cout /*<< "B: " */<< it->second;
    
    it= my_morse.find("1");
    if( it != my_morse.end() ) std::cout /*<< "C: " */<< it->second;
    
    it= my_morse.find("000");
    if( it != my_morse.end() ) std::cout /*<< "C: " */<< it->second;
    
    it= my_morse.find("111");
    if( it != my_morse.end() ) std::cout /*<< "C: " */<< it->second;
        
  
return 0;
}
Last edited on
I don't think that a map is the best container here.
if the input string has no separators you need to order the morse code according to its length (the longest first) and then match the part of string against them one after another. Otherwise you would have a lot of 'e's
closed account (GL1Rko23)
The input string does have seerators; they are 000 between letters and 0000000 between words, the keys take care of the 0's inbetween the letter itself
closed account (o1vk4iN6)
You can use a binary tree to do the decoding, 0/1 for left/right and at the end of a tree have some letter as an output. Some encoding that is similar to this:

http://en.wikipedia.org/wiki/Huffman_coding

my_morse["1"] = 'e';

That for example, how do can decode if 1 is set to e. How are you going to distinguish e from any other letter if all other letters also start with 1 ? Your "morse" code is ambiguous.
Last edited on
closed account (GL1Rko23)
The Binary tree is a good idea,I will look into that as well. The way I'd be able to distinguish the e is because after 1 which is e there would be three 000's meaning the letter had ended and there will never be a case where three consecutive 000's does not mean to look for the next letter.

Within the morse I have (1)000(next letter)(1)(000); this would be to e's and how they would be distinguished.
Last edited on
> The input string does have seerators;
> they are 000 between letters and 0000000 between words

So what is the problem?

Something like this should suffice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> split( const std::string& code, const std::string& sep )
{
    std::vector<std::string> result ;

    std::string::size_type pos = 0 ;
    auto f = code.find(sep) ;
    while( f != std::string::npos )
    {
        result.emplace_back( code.begin()+pos, code.begin()+f ) ;
        pos = f + sep.size() ;
        f = code.find( sep, pos ) ;
    }
    result.push_back( code.substr(pos) ) ;

    return result ;
}

int main()
{
    const std::string word_seperator = "0000000" ;
    const std::string letter_seperator = "000" ;

    const std::string msg = "111000101010100010000000"
                              "1110111010111000101011100010100011101011101000111010111" ;
    std::cout << "msg: " << msg << '\n' ;
    // for each word in msg
    for( const std::string& word : split( msg, word_seperator ) )
    {
        std::cout << "    word: " << word << '\n' ;
        // for each letter in word
        for( const std::string& letter : split( word, letter_seperator ) )
        {
            std::cout << "        letter: " << letter << '\n' ;
            // look up letter in map, get the character
        }
    }
}

http://ideone.com/0yQZ7q
closed account (GL1Rko23)
@JLBorges, Thank you, I used the basis of your code and got everything i wanted perfectly, It was perfect to pass letter into the map iterator
Topic archived. No new replies allowed.