简体   繁体   English

将值分别插入地图

[英]Inserting values into map individually

Hello, 你好,
I am making a program in C++ in which i have used a std::map to insert two values (char, int) in which every char (a,b,c,d) is related to it's occurence in the alphabet system(1,2,3,4), so that, if I sort the alphabets, their numbers also get sorted. 我正在用C ++编写一个程序,其中我使用了std :: map插入两个值(char,int),其中每个char(a,b,c,d)与它在字母表系统中的出现有关(1 ,2,3,4),这样,如果我对字母进行排序,它们的数字也会被排序。 I know a better way is by Substracting the ACSII values and all, but this is part of a bigger code and I am interested in maps. 我知道一个更好的方法是减去ACSII值和所有值,但这是更大代码的一部分,我对地图感兴趣。


Now, in my program, the user enters the characters {a, b, c} in an unsorted way and then(after entering the characters) they enter the number of their choice for every aphabet respective to what they entered before (something like custom alphabet system). 现在,在我的程序中,用户以未排序的方式输入了字符{a,b,c},然后(在输入字符之后)他们为每个字母缩写输入了与之前输入的字符相对应的选择编号(类似于自定义字母系统)。
I know how to enter values in a map using 我知道如何使用

 MyMap.insert(pair <char, int> ('a', 1)); 

but in this case the user enters all the characters first and then all the numbers so I can't use the pair function. 但是在这种情况下,用户首先输入所有字符,然后输入所有数字,所以我不能使用配对功能。 I also don't want to store the values in an array and then after receiving the int value, inserting them into the map. 我也不想将值存储在数组中,然后在接收到int值之后,将它们插入到映射中。 I want to know if there is a direct method to insert the values of the chars first and then their corresponding values(If possible, a better way than inserting the {char value, 0} and then editing the 0 to the number.). 我想知道是否存在直接插入char的值然后再插入其对应值的直接方法(如果可能,比插入{char value,0}然后将0编辑为数字更好的方法。)。

Any help is greatly appreciated!! 任何帮助是极大的赞赏!!

first you could use some error indication for your map, ie. 首先,您可以在地图上使用一些错误指示,即 -1 to indicate that no number was given for the character. -1表示没有为该字符提供数字。 Then you could add values wihtout knowing the actual mapped value and change them later. 然后,您可以在不知道实际映射值的情况下添加值,并在以后更改它们。 However maps wont store values in the order you inserted them, see binary tree . 但是,地图不会按照插入顺序存储值,请参阅二叉树 Hence you would have to store the order of incoming characters sperately. 因此,您将必须分别存储输入字符的顺序。 But if you store the characters anyway and require a mapping for each of them there is no point in creating map entries wihtout having sufficient information about their values. 但是,如果您仍然存储字符并要求为每个字符映射,那么创建具有足够有关其值的信息的映射条目就毫无意义。 I would suggest to first store all characters and insert all values while you are reading the user defined value for this character. 我建议先存储所有字符并在读取该字符的用户定义值时插入所有值。 Here is a small example. 这是一个小例子。 You can try it on c++ shell . 您可以在c ++ shell上尝试。

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
    const size_t numberOfCharacters = 3;

    cout << "Please provide the characters you want to map" << endl;
    vector<char> characterSequence{numberOfCharacters, -1};
    for(size_t i = 0; i < numberOfCharacters; ++i) {
        cout << i << ": ";
        cin >> characterSequence[i];
    }

    map<char, int> userDefinedCharacterMapping;
    cout << endl << "Please provide the characters mapped values" << endl;
    for(size_t i = 0; i < numberOfCharacters; ++i) {
        const auto iCharacter = characterSequence[i];
        cout << iCharacter << ": ";
        cin >> userDefinedCharacterMapping[iCharacter];
    }

    cout << endl << "Your result:" << endl;
    for(const auto& iUserDefinedCharacterMapping : userDefinedCharacterMapping) {
        cout << iUserDefinedCharacterMapping.first << " -> " << iUserDefinedCharacterMapping.second << endl;
    }

    return 0;
}

Here a sample output where map iterates the characters in a different order then entered. 这里是一个示例输出,其中map以不同的顺序迭代字符,然后输入字符。

Please provide the characters you want to map
0: b
1: q
2: a

Please provide the characters mapped values
b: 0
q: -10
a: 13

Your result:
a -> 13
b -> 0
q -> -10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM