C++ 如何在C+中应用字符串变量计数的概念+;
下面的程序可以计算数组中整数的频率 如何将此概念应用于字符串变量,因为字符串也是后端的数组C++ 如何在C+中应用字符串变量计数的概念+;,c++,counting,C++,Counting,下面的程序可以计算数组中整数的频率 如何将此概念应用于字符串变量,因为字符串也是后端的数组 using namespace std; int counter[10]={0,0,0,0,0,0,0,0,0,0}; int arr [9][9],x; int main() { srand(time(NULL)); cout<<"enter the array \n"; for(int i=0;i<9;i++){ for(int j=0;j
using namespace std;
int counter[10]={0,0,0,0,0,0,0,0,0,0};
int arr [9][9],x;
int main()
{
srand(time(NULL));
cout<<"enter the array \n";
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
arr[i][j]=rand()%10;
}
}
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
counter[arr[i][j]]++;
}
}
for(int j=0;j<10;j++){
cout<<j<<" : "<< counter[j]<<endl;
}
return 0;
}
使用名称空间std;
int计数器[10]={0,0,0,0,0,0,0,0,0};
int arr[9][9],x;
int main()
{
srand(时间(空));
cout以下是如何从任何事件中计算任何事件的发生率:
密码
它的返回类型是从它的实现中推断出来的。扰流板警报:它是一个std::map
of(值计数器,出现此值)
引用
由两个定义范围的迭代器调用,通常在容器C
上调用begin(C)
和end(C)
std::for_each(begin, end, //...
对于范围中的每个元素
[&result](auto const& item){ //...
…执行以下处理
++result[item]; });
…增加值项
的出现计数,如果是第一个,则从零开始
这不是一个有效的实现,因为它复制它计数的值。对于整数、字符等,这是完美的,但对于复杂类型,您可能需要改进此实现
它与通用容器和标准容器兼容。您可以计算任何可计算的事件。以下是如何从任何事件中计算任何事件的发生次数:
密码
它的返回类型是从它的实现中推断出来的。扰流板警报:它是一个std::map
of(值计数器,出现此值)
引用
由两个定义范围的迭代器调用,通常在容器C
上调用begin(C)
和end(C)
std::for_each(begin, end, //...
对于范围中的每个元素
[&result](auto const& item){ //...
…执行以下处理
++result[item]; });
…增加值项
的出现计数,如果是第一个,则从零开始
这不是一个有效的实现,因为它复制它计数的值。对于整数、字符等,这是完美的,但对于复杂类型,您可能需要改进此实现
它与通用和标准容器兼容。您可以计算任何可计算的值。如果我理解正确,您希望计算字符串的出现次数。STL容器映射用于此目的。以下是示例代码
#include<iostream>
#include<map>
#include<string>
#include<vector>
int main()
{
std::vector<std::string> arrayString;
std::map<std::string, int> counter;
std::map<std::string, int>::iterator it;
arrayString.push_back("Hello");
arrayString.push_back("World");
arrayString.push_back("Hello");
arrayString.push_back("Around");
arrayString.push_back("the");
arrayString.push_back("World");
// Counting logic
for(std::string strVal : arrayString)
{
it = counter.find(strVal);
if(it != counter.end())
it->second += 1; // increment count
else
counter.insert(std::pair<std::string, int>(strVal, 1)); // first occurrence
}
// Results
for(std::map<std::string, int>::iterator it = counter.begin(); it != counter.end(); ++it)
std::cout << it->first << ": " << it->second << std::endl;
return 0;
}
如果我理解正确,您需要计算字符串的出现次数。STL容器映射对于此目的非常有用。以下是示例代码
#include<iostream>
#include<map>
#include<string>
#include<vector>
int main()
{
std::vector<std::string> arrayString;
std::map<std::string, int> counter;
std::map<std::string, int>::iterator it;
arrayString.push_back("Hello");
arrayString.push_back("World");
arrayString.push_back("Hello");
arrayString.push_back("Around");
arrayString.push_back("the");
arrayString.push_back("World");
// Counting logic
for(std::string strVal : arrayString)
{
it = counter.find(strVal);
if(it != counter.end())
it->second += 1; // increment count
else
counter.insert(std::pair<std::string, int>(strVal, 1)); // first occurrence
}
// Results
for(std::map<std::string, int>::iterator it = counter.begin(); it != counter.end(); ++it)
std::cout << it->first << ": " << it->second << std::endl;
return 0;
}
数字是来自字母表[0..9]的一系列项目。字符串是来自稍大的字母表的一系列项目。假设字符串是常规ASCII,则可以使用[0..127]作为字母表。这仍然包括一些无法打印的字符(所有笑话:一旦你在std::string
上计算完char
频率,你基本上可以在任何类型上进行,比如std::basic_string
:你的目标是计算不同字符串或单个字符串中字符的出现次数吗?我想计算stringA中每个字符的出现频率数字是来自字母表[0..9]的一系列项目。字符串是来自稍大的字母表的一系列项目。假设字符串是常规ASCII,则可以使用[0..127]作为字母表。这仍然包括一些无法打印的字符(所有笑话:一旦你计算完std::string
上的char
频率,你基本上可以在任何类型上进行,比如std::basic_string
:你的目标是计算不同字符串或单个字符串中字符的出现次数吗?我想计算stringF中每个字符的出现频率或较小且已知的“连续”字母表(如数字/ascii字母表),std::array
甚至可能取代std::map
。注意:我本来想找到这个问题的副本,但没有找到一个足够通用或有价值的副本。这就是我写这个答案的原因。现在,我认为有一个可以作为这个答案的副本关闭。作为这个答案的作者,我不够客观。@Jarod42它可以通过专门化来完成:引用(/*…*/)
返回std::array
和引用(/*…*/)
astd::map
?
=起点,如'a'
用于在[a..z]中计数
。如果在map
s元素上添加注释,则答案会更加重复sorted@Mihai好问题。你会在这里找到答案(还有更多:。简短回答:因为我希望编译器将std::iterator\u traits::value\u type
解析为一个类型(它可能是一个值,它不知道)。对于小的和已知的“连续”字母表(作为数字/ascii字母表),std::array
甚至可能取代std::map
。注意:我本来想找到这个问题的副本,但没有找到一个足够通用或有价值的副本。这就是我写这个答案的原因。现在,我认为有一个可以作为这个答案的副本关闭。作为这个答案的作者,我不够客观。@Jarod42它可以通过专门化来完成:引用(/*…*/)
返回std::array
和引用(/*…*/)
astd::map
?=起点,如'a'
用于在[a..z]中计数
。如果在map
s元素上添加注释,则答案会更加重复sorted@Mihai好问题。你会在这里找到答案(还有更多:。简短回答:因为我希望编译器将std::iterator\u traits::value\u type
解析为一个类型(它可能是一个值,它不知道)。您可以像我一样简化代码:它为未知键创建值并对其进行零初始化。您可以像我一样简化代码:它为未知键创建值并对其进行零初始化。
#include<iostream>
#include<map>
#include<string>
#include<vector>
int main()
{
std::vector<std::string> arrayString;
std::map<std::string, int> counter;
std::map<std::string, int>::iterator it;
arrayString.push_back("Hello");
arrayString.push_back("World");
arrayString.push_back("Hello");
arrayString.push_back("Around");
arrayString.push_back("the");
arrayString.push_back("World");
// Counting logic
for(std::string strVal : arrayString)
{
it = counter.find(strVal);
if(it != counter.end())
it->second += 1; // increment count
else
counter.insert(std::pair<std::string, int>(strVal, 1)); // first occurrence
}
// Results
for(std::map<std::string, int>::iterator it = counter.begin(); it != counter.end(); ++it)
std::cout << it->first << ": " << it->second << std::endl;
return 0;
}
// Counting logic
for(std::string strVal : arrayString)
{
++counter[strVal]; // first time -> init to 0 and increment
}