Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数对字符串中的所有字符进行计数-C++; 我想在C++中编写一个函数,它对字符串中的所有字符进行计数。 我有一个名为input的字符串,程序的用户可以在其中输入一个句子, 重要的字母存储在字符串字母表中,如下所示: string alphabet {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; #include <unordered_map> #include <algorithm> #include <string> #include <iostream> int main() { std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; std::string input = "hello world"; std::unordered_map<char, unsigned int> counts; std::unordered_map<char, unsigned int> counts2; std::for_each(std::begin(input), std::end(input), [&counts](char c) { counts[c]++; }); std::for_each(std::begin(alphabet), std::end(alphabet), [&counts, &counts2] (char c) { const auto& it = counts.find(c); if( it != counts.end()) counts2.insert(*it); }); for(auto& kv: counts2) { std::cout << kv.first << ": " << kv.second << "\n"; } return 0; } std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; std::string input = "MISSISSIPPI yukon."; // First insert the characters that we want to count. std::unordered_map<char, size_t> map; for (auto ch : alphabet) map[ch] = 0; // Then count ONLY the inserted characters. for (auto ch : input) { auto it = map.find(ch); if (it != map.end()) ++it->second; } for (auto pair : map) if (pair.second > 0) std::cout << '\'' << pair.first << "\'\t" << pair.second << std::endl;_C++ - Fatal编程技术网

函数对字符串中的所有字符进行计数-C++; 我想在C++中编写一个函数,它对字符串中的所有字符进行计数。 我有一个名为input的字符串,程序的用户可以在其中输入一个句子, 重要的字母存储在字符串字母表中,如下所示: string alphabet {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; #include <unordered_map> #include <algorithm> #include <string> #include <iostream> int main() { std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; std::string input = "hello world"; std::unordered_map<char, unsigned int> counts; std::unordered_map<char, unsigned int> counts2; std::for_each(std::begin(input), std::end(input), [&counts](char c) { counts[c]++; }); std::for_each(std::begin(alphabet), std::end(alphabet), [&counts, &counts2] (char c) { const auto& it = counts.find(c); if( it != counts.end()) counts2.insert(*it); }); for(auto& kv: counts2) { std::cout << kv.first << ": " << kv.second << "\n"; } return 0; } std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; std::string input = "MISSISSIPPI yukon."; // First insert the characters that we want to count. std::unordered_map<char, size_t> map; for (auto ch : alphabet) map[ch] = 0; // Then count ONLY the inserted characters. for (auto ch : input) { auto it = map.find(ch); if (it != map.end()) ++it->second; } for (auto pair : map) if (pair.second > 0) std::cout << '\'' << pair.first << "\'\t" << pair.second << std::endl;

函数对字符串中的所有字符进行计数-C++; 我想在C++中编写一个函数,它对字符串中的所有字符进行计数。 我有一个名为input的字符串,程序的用户可以在其中输入一个句子, 重要的字母存储在字符串字母表中,如下所示: string alphabet {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; #include <unordered_map> #include <algorithm> #include <string> #include <iostream> int main() { std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; std::string input = "hello world"; std::unordered_map<char, unsigned int> counts; std::unordered_map<char, unsigned int> counts2; std::for_each(std::begin(input), std::end(input), [&counts](char c) { counts[c]++; }); std::for_each(std::begin(alphabet), std::end(alphabet), [&counts, &counts2] (char c) { const auto& it = counts.find(c); if( it != counts.end()) counts2.insert(*it); }); for(auto& kv: counts2) { std::cout << kv.first << ": " << kv.second << "\n"; } return 0; } std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; std::string input = "MISSISSIPPI yukon."; // First insert the characters that we want to count. std::unordered_map<char, size_t> map; for (auto ch : alphabet) map[ch] = 0; // Then count ONLY the inserted characters. for (auto ch : input) { auto it = map.find(ch); if (it != map.end()) ++it->second; } for (auto pair : map) if (pair.second > 0) std::cout << '\'' << pair.first << "\'\t" << pair.second << std::endl;,c++,C++,以及用于存储字母出现频率的向量,例如a位于位置0,B位于位置0,等等 vector<long> letterCount (26); 例如,你看,字母“L”在字符串中包含了两次,但“L”的结果是24,因为“L”在字母表中位于12处 如果你知道我的问题是什么,请帮助我 编辑:我找到了一种有效的方法,至少部分有效: long countLetters(int& p) { for(size_t i = 0; i < input.length(); ++i) {

以及用于存储字母出现频率的向量,例如a位于位置0,B位于位置0,等等

vector<long> letterCount (26);
例如,你看,字母“L”在字符串中包含了两次,但“L”的结果是24,因为“L”在字母表中位于12处

如果你知道我的问题是什么,请帮助我

编辑:我找到了一种有效的方法,至少部分有效:

long countLetters(int& p) {
   for(size_t i = 0; i < input.length(); ++i) {
      for(size_t j = 0; j < alphabet.length(); ++j) {
        letterCount.at(j) = count(input.begin(), input.end(), alphabet.at(j));
      }
   }
   return letterCount.at(p);
 }
长计数字母(int&p){
对于(大小i=0;i
但当输入两个或多个单词时,该函数只计算出第一个单词中出现的字母。我如何分析更多的单词


编辑:之前我有
cin>>输入
但是
getline(cin,输入)是对的。

你在做某种奇怪的双循环。相反,在单个循环中对字符串进行迭代,并在正确的组中对其进行计数:

for (int i = 0; i < input.length(); i++) {
    char c = input[i];
    if (c < 'A' || c > 'Z') continue;
    countLetters[c-'A'] += 1;
}
for(int i=0;i'Z')继续;
计数字母[c-'A']+=1;
}

正如@KillianDS在评论中提到的,如果你想要一个通用的解决方案(即一个可以变化的“字母表”),最简单的方法可能是统计每个可能字符的出现次数,然后根据你的实际字母表过滤:

// count every possible character
std::array<size_t, (1 << (8 * sizeof(char)))> countChars;
countChars.fill(0);
for (auto i = input.begin(); i != input.end(); ++i)
    countChars[*i]++;
// extract only the ones you're interested in
std::vector<size_t> countLetters;
for (auto i = alphabet.begin(); i != alphabet.end(); ++i)
    countLetters.push_back(countChars[*i]);
//计算每个可能的字符

std::array我将通过以下两个步骤完成此操作:

string alphabet {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
#include <unordered_map>
#include <algorithm>
#include <string>
#include <iostream>

int main()
{
    std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
    std::string input = "hello world";
    std::unordered_map<char, unsigned int> counts;
    std::unordered_map<char, unsigned int> counts2;
    std::for_each(std::begin(input), std::end(input), [&counts](char c) {
        counts[c]++;
    });
    std::for_each(std::begin(alphabet), std::end(alphabet), [&counts, &counts2] (char c) {
        const auto& it = counts.find(c);
        if( it != counts.end()) counts2.insert(*it);        
    });
    for(auto& kv: counts2)
    {
        std::cout << kv.first << ": " << kv.second << "\n";
    }
    return 0;
}
std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string input = "MISSISSIPPI yukon.";

// First insert the characters that we want to count.
std::unordered_map<char, size_t> map;
for (auto ch : alphabet)
    map[ch] = 0;

// Then count ONLY the inserted characters.
for (auto ch : input) {
    auto it = map.find(ch);
    if (it != map.end())
        ++it->second;
}

for (auto pair : map)
    if (pair.second > 0)
        std::cout << '\'' << pair.first << "\'\t" << pair.second << std::endl;
当然,如果您的输入集具有某些数学属性(如精确范围),您可以使用Tom van der Woerdt的解决方案,因为这将是
O(N)
,并且您的速度无法超过此值。

您也可以这样做:

 char   *x = "cmnasdkASFSAFASDisdajkhasdfjqwedz" ; // work UPPER , lower
 static int  c[26] ;

 int main ( void ) {

 while ( *x )   {
     int ndx= *x - (islower(*x) ? 'a' : 'A') ;
     c[ ndx] += isalpha(*x++) ? 1 : 0 ;
 }
 for ( int i =0;i<26;i++)   printf ( "\n%d", c[i] );  
}
char*x=“cmnasdkasfsafsasdisdajkhasdfjqwedz”//上下工作
静态int c[26];
内部主(空){
而(*x){
int ndx=*x-(岛低(*x)‘a’:‘a’;
c[ndx]+=isalpha(*x++)?1:0;
}

对于(int i=0;i你可以这样做:

string alphabet {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
#include <unordered_map>
#include <algorithm>
#include <string>
#include <iostream>

int main()
{
    std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
    std::string input = "hello world";
    std::unordered_map<char, unsigned int> counts;
    std::unordered_map<char, unsigned int> counts2;
    std::for_each(std::begin(input), std::end(input), [&counts](char c) {
        counts[c]++;
    });
    std::for_each(std::begin(alphabet), std::end(alphabet), [&counts, &counts2] (char c) {
        const auto& it = counts.find(c);
        if( it != counts.end()) counts2.insert(*it);        
    });
    for(auto& kv: counts2)
    {
        std::cout << kv.first << ": " << kv.second << "\n";
    }
    return 0;
}
std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string input = "MISSISSIPPI yukon.";

// First insert the characters that we want to count.
std::unordered_map<char, size_t> map;
for (auto ch : alphabet)
    map[ch] = 0;

// Then count ONLY the inserted characters.
for (auto ch : input) {
    auto it = map.find(ch);
    if (it != map.end())
        ++it->second;
}

for (auto pair : map)
    if (pair.second > 0)
        std::cout << '\'' << pair.first << "\'\t" << pair.second << std::endl;
…因为我们只计算字母表中的字符


如果您希望保证结果有序(在上面的示例中,它们是意外订购的),请将
std::unordered_map
替换为
std::map

这是我解决问题的版本,并按降序打印结果

void printNumofLetterinString(std::string sentence){
    int frequencyArray[26];         //FrequencyArray is used to store the frequency
    for(int i=0;i<26;i++){          //of the letters and Initialize 
        frequencyArray[i] = 0;      //frequencyArray to all zero.
    }
    int ascii;
    for(int i=0;i<sentence.length();i++){
        if(!isalpha(sentence[i])){
            continue;
        }
        ascii = tolower(sentence[i]) - 'a';   //Convert A-Za-z to number between 0-25.
        frequencyArray[ascii]++;
    }
    for(int i=0;i<26;i++){              //Find the biggest number in frequencyArray     
        int max = frequencyArray[0];    //print it, then set it to zero  
        int index = 0;                  //and find the next biggest number.
        for(int j=0;j<26;j++){
            if(frequencyArray[j] > max){
                max = frequencyArray[j];
                index = j;
            }
        }
        if(max == 0){
            break;
        }
        char c = index + 'a';
        std::cout<<c<<" "<<max<<std::endl;
        frequencyArray[index] = 0;
    }
}
chararr[]={“aaabbaccdaadac”};
地图我的地图;
对于(int i=0;等秒;
插入(对(arr[i],it->second));
}
迭代器mapit;
对于(mapit=mymap.begin();mapit!=mymap.end();mapit++)
{
无法测试此宏

#define FOR_ALL(cont , block)\
for (const auto &itr : cont)\
    block;
这部分代码

map<char, int> countLetters;
FOR_ALL(str, countLetters[itr]++);
map countlets;
对于所有(str,countLetters[itr]++);
以及打印结果

for (const auto &element : m)
    cout << element.first << ' ' << element.second<<endl;
for(常量自动和元素:m)
不能包含
#包括
使用名称空间std;
int main(){
char-str[50];
cin.getline(str,50);
int-arr[1234]={0};
///每个字符的提取
int i=0;
while(str[i]!='\0'){
arr[str[i]-“”++;/*将字符隐式转换为整数类型
并在该位置存储ASCII字符的频率
数组的位置。“”空格字符只是一个
参考点…*/
i++;
}
///显示字符频率

对于(i=0;i,该字符串可以作为用户的参数

cin >> inputString;

unordered_map<char, int> characterMap;

for (char c : inputString){
    characterMap[c]++;
}
for (std::pair<char, int> characterCount : characterMap) { // Alternatively use 'auto' as type
    cout << characterCount.first << " count: " << characterCount.second << endl;
}
cin>>输入字符串;
无序映射字符映射;
for(字符c:inputString){
字符映射[c]++;
}
对于(std::pair characterCount:characterMap){//或者使用“auto”作为类型

如果(c<'A'| c>'Z')
,为什么不
?请重新阅读代码。实际上这一点都不奇怪(尽管第二个递增循环没有用),它只是一个正交循环approach@stefan这很奇怪,因为它比它应该的复杂得多。为什么不使用
if(!std::isalpha(c))
而不是
if(c<'a'| c>'Z')
?他从不说实际的问题只包含字母表,只说有一个带有重要字符的给定字符串,这不是通用的解决方案。你从不修改
字母计数
,你使用一个未知的对象
字母计数
,看起来很像你的函数名。第二:你为什么要检查whol如果你只对
字母计数[p]
感兴趣,最好在答案中添加一些描述。
cin >> inputString;

unordered_map<char, int> characterMap;

for (char c : inputString){
    characterMap[c]++;
}
for (std::pair<char, int> characterCount : characterMap) { // Alternatively use 'auto' as type
    cout << characterCount.first << " count: " << characterCount.second << endl;
}