如何在C ++中将计数出现次数的概念应用于字符串变量 [英] How to apply the concept of counting occurrences on strings variables in C++

查看:50
本文介绍了如何在C ++中将计数出现次数的概念应用于字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序可以计算数组
中整数的频率,因为字符串也是后端的数组,因此如何将这个概念应用于字符串变量

 使用命名空间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<输入数组\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 ++){{b $ b for(int j = 0; j< 9; j ++){ b $ b counter [arr [i] [j]] ++;

}
}

for(int j = 0; j< 10; j ++){
cout<< j<<: <<计数器[j]< endl;

}
返回0;
}


解决方案

这是一个可以计数的方式发生的任何事情:


代码


  #include< iterator> 
#include< map>
#include< algorithm>

template< class InputIt>
自动出现
次(InputIt开始,InputIt结束)
{
std :: map< typename std :: iterator_traits< InputIt> :: value_type,std :: size_t>结果;
std :: for_each(begin,end,[& result](auto const& item){++ result [item];});
的返回结果;
}


用法


  #include< string> 
#include< iostream>

int main()
{
auto text = std :: string { Hello,World!};
自动occ =出现次数(开始(文本),结束(文本));
std :: cout<< occ [’l’]<< ‘n’; //输出3
}

实时演示


说明


  template< class InputIt> 

这是在任何输入迭代器上进行迭代的通用(模板)函数。

  auto 

其返回类型是从其实现中推断出来的。剧透警报:它是 std :: map 的(值计数器,该值的出现)。

 出现次数(InputIt开始,InputIt结束)

出现次数通过几个定义范围的迭代器进行调用,通常在 begin(C) end(C)上调用您的容器 C

  std :: for_each(开始,结束,// .. 。

对于范围中的每个元素...

  [& result](auto const& item){// ... 

...执行以下处理...

  ++ result [item];}); 

...增加值 item 的出现次数,从零开始(如果是第一个,则从零开始)。对于整数,字符等,完美无缺,但对于复杂类型,您可能需要改进此实现。


它与通用容器和标准容器兼容。您可以指望任何可迭代的事物。


following program ca calculate the frequency of ints in an array how to apply this concept on string variable because a string is also an array on the back end

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;
}

解决方案

Here is how one can count occurrences of anything from anything:

Code

#include <iterator>
#include <map>
#include <algorithm>

template<class InputIt>
auto
occurrences(InputIt begin, InputIt end)
{
    std::map<typename std::iterator_traits<InputIt>::value_type, std::size_t> result;
    std::for_each(begin, end, [&result](auto const& item){ ++result[item]; });
    return result;
}

Usage

#include <string>
#include <iostream>

int main()
{
    auto text = std::string{"Hello, World!"};
    auto occ = occurrences(begin(text), end(text));
    std::cout << occ['l'] << '\n'; // outputs 3
}

Live demo

Explanation

template<class InputIt>

This is a generic (template) function iterating over any input iterator.

auto

Its return type is inferred from its implementation. Spoiler alert: it is a std::map of (value counter, occurrence of this value).

occurrences(InputIt begin, InputIt end)

occurrences is called with a couple of iterators defining a range, generally calling begin(C) and end(C) on your container C.

std::for_each(begin, end, //...

For each element in the range...

[&result](auto const& item){ //...

...execute the following treatment...

++result[item]; });

...increment the occurrence count for the value item, starting with zero if its the first.

This is not an efficient implementation since it copies the values it counts. For integers, characters, etc. its perfect but for complex types you might want to improve this implementation.

It's generic and standard container compatible. You could count anything iterable.

这篇关于如何在C ++中将计数出现次数的概念应用于字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆