我需要將字母轉換為字符字典。下面是一個例子:
letter
l: 1
e: 2
t: 2
r: 1
我做了一些研究并找到了這個有用的答案,但那是使用getline()空格并用空格分隔單詞。由于我試圖按字符拆分,因此我認為我不能使用,getline()因為''它不是有效的拆分字符。我可以轉換為一個char*陣列,但我不確定這會讓我在哪里。
這在其他語言中相當容易,所以我認為在 C 中它不會太糟糕。我希望會有類似的my_map[key] 東西。在 Go 中,我會這樣寫
// Word map of string: int values
var wordMap = make(map[string]int)
// For each letter, add to that key
for i := 0; i < len(word); i {
wordMap[string(word[i])]
}
// In the end you have a map of each letter.
我怎么能在 C 中應用它?
uj5u.com熱心網友回復:
我怎么能在 C 中應用它?
它看起來可能與您的 Go 代碼非常相似。
// Word map of char: int values
// (strings would be overkill, since you know they are a single character)
auto wordMap = std::map<char,int>{};
// For each letter, add to that key
for ( char c : word )
wordMap[c] ;
}
uj5u.com熱心網友回復:
這是 Drew Dormann 回答的 unicode 版本:
#include <locale>
#include <codecvt>
std::string word = "some unicode: こんにちは世界";
std::map<char32_t, uint> wordMap;
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
for (auto c : converter.from_bytes(word)) {
wordMap[c] ;
}
for (const auto [c, v] : wordMap) {
std::cout << converter.to_bytes(c) << " : " << v << std::endl;
}
uj5u.com熱心網友回復:
我寫了一篇關于這個的文章,可以在這里查看。下面我給出了該程式的 2 個版本。版本 1按字母順序跟蹤字符數。但有時(以防萬一)您需要按插入順序排列的字符數,以便您可以使用版本 2。
版本1:獲取字符數在?字母順序
#include <iostream> //needed for std::cout, std::cin
#include <map> //needed for std::map
#include <iomanip> //needed for formating the output (std::setw)
int main()
{
std::string inputString; //user input will be read into this string variable
std::cout << "Enter a string: " << std::endl;
std::getline(std::cin, inputString);
//this map maps the char to their respective count
std::map < char, int > charCount;
//iterate through the inputString
for (char & c: inputString)
{
charCount[c] ;//increment the count for character c
}
std::cout << "Total unique characters are: " << charCount.size() << std::endl;
std::cout << "------------------------------------" << std::endl;
std::cout << "Character" << std::setw(10) << "Count" << std::endl;
std::cout << "------------------------------------" << std::endl;
for (std::pair < char, int > pairElement: charCount)
{
std::cout << std::setw(4) << pairElement.first << std::setw(13) << pairElement.second << std::endl;
}
return 0;
}
版本 2:獲取i?n?s?e?r?t?i?o?n??o?r?d?e?r?中的字符數
#include <iostream>
#include <map>
#include <iomanip>
int main()
{
std::string inputString;
std::cout << "Enter a string: " << std::endl;
std::getline(std::cin, inputString);
std::map < char, int > charCount;
for (char & c: inputString)
{
charCount[c] ;
}
std::cout << "Total unique characters are: " << charCount.size() << std::endl;
std::cout << "------------------------------------" << std::endl;
std::cout << "Character" << std::setw(10) << "Count" << std::endl;
std::cout << "------------------------------------" << std::endl;
std::size_t i = 0;
//just go through the inputString instead of map
for(char &c: inputString)
{
std::size_t index = inputString.find(c);
if(index != inputString.npos && (index == i)){
std::cout << std::setw(4) << c << std::setw(13) << charCount.at(c)<<std::endl;
}
i;
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/368288.html
上一篇:如何比較C 中的兩個地圖迭代器?
