-2

I dont know how to include a map or a pair in this code below to print ou a list in format A:1 B:2 C:3

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

int main() {
    // your code goes here
    char letter = 'A';
    typedef pair<char, int> LettreValues;
    for (int i = 1; i < 27; ++i)
    {       
        LettreValues[letter]=i;
        static_cast<char>(letter + 1);
    }
    for(auto elem : LettreValues)
    {
       std::cout << elem.first << " " << elem.second.first << " " << elem.second.second << "\n";
    }

    return 0;
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Tahtoh
  • 75
  • 1
  • 10

2 Answers2

0

You're doing a number of things wrong.

char letter = 'A';

so far so good.

typedef pair<char, int> LettreValues;

Now you've gone south. You've defined a type alias ('typedef') for std::pair that is called "LettreValues".

But LettreValues is a type - not an instance of that type.

for (int i = 1; i < 27; ++i)

Since i starts at 1, the first value you insert into your map will be 'A' + 1.

LettreValues[letter]=i;

This attempts to use LettreValues as a variable, which it is not; it tries to use it as a map, which is it not - it's a pair.

static_cast<char>(letter + 1);

This adds one to letter, converts it to a char, and discards it.

I think perhaps you were intending something like this:

// Declare an alias for std::map<char, int>.
using LettreValues = std::map<char, int>;
// Now declare a variable of this type
LettreValues letterValues;

// the type alias step is completely optional, we
// could have just written
// std::map<char, int> letterValues;

for (int i = 0; i < 26; ++i) {
    letterValues[letter] = i;
    letter++;
}

for (auto& el : letterValues) {
    std::cout << el.first << " " << el.second << "\n";
}

Live demo: http://ideone.com/7ZA5Bk

kfsone
  • 23,617
  • 2
  • 42
  • 74
  • exactly thanks mate – Tahtoh Jul 11 '16 at 11:43
  • Can you check my code right here, i tried getting right, creating multiple functions to simplfy the task, but i always returns the wrong result. Here is the link to the problem http://train.usaco.org/usacoprob2?a=zeWky61KJoG&S=ride And here is my solution http://ideone.com/a9YHl6 – Tahtoh Jul 11 '16 at 15:44
  • Your use of a map is over-engineering, you can just use `input[i] - 'A' + 1`, also calling `returnMap()` in the loop means recreating the map for each and every letter. `sizeof(string)` returns the size of a string object, not the length of the string (that's very pythoneseque of you). To get the length of a string use `input.length()`. Lastly, the values you are calculating are multiples of 10. Any multiple of 10 modulo 10 is 0. The comments in your code indicate it should be 'mod 47' not 'mod 10'. http://ideone.com/yBWr8l – kfsone Jul 11 '16 at 16:56
  • @tahtoh see my fork: http://ideone.com/yBWr8l – kfsone Jul 11 '16 at 16:56
  • Thanks for the quick replies @ksfone , i'm trying to learn the basic syntax, kindof hard cause i need to try everything 1by1 to inderstand how it works in real life situation, and this is before i get into DP , thanks for the help, the next stop is learning the mindblowing hard as hell recursion hehe. – Tahtoh Jul 11 '16 at 17:02
-1

Try inserting into pair using std::make_pair as below.

char letter = 'A';
    typedef pair<char, int> lettervalues;   
    for(int i = 1;i<27;i++)
    {
        lettervalues l = std::make_pair<char, int>(letter,i);
        letter = static_cast<char>(letter + i);
    }