繁体   English   中英

C ++-开发自己的std :: count_if版本

[英]C++ - Developing own version of std::count_if

对于一项任务,我正在做一些简单的数据采样,以确定哪些采样包含音频,该音频计数能量的总数。 我一直在研究std::count_if函数,尽管这在某种程度上适合我的需求,例如:

int foo = std::count_if(
               std::begin(samples), 
               std::end(samples),
               containsSound);

这会计算包含声音的样本总数,但不会指示包含声音的样本。 我想出了这个解决方案:

std::vector<std::string> sound_files = myLib::count_sample_if(
                                              std::begin(samples),
                                              std::end(samples),
                                              samples.DirName, 
                                              containsSOund);

然后,它将存储并将samples.DirName推入vector ,然后可以使用该vector仅存储我选择的样本集。

我的问题是这是否容易实现?

如果您只需要可读性/开发速度,并且不关心性能,那么可以轻松地使用std::copy_ifstd::transform获得所需的内容:

std::vector<Song> songs;
std::vector<Song> filtered;
std::vector<std::string> transformed;

std::copy_if(songs.begin(), songs.end(), filtered.begin(), [](const Song &song) { return whatever you need; });
std::transform(filtered.begin(), filtered.end(), transformed.begin(), [](const Song &song) { return song.sample; });

或者您可以使用std::for_each

std::vector<Song> songs;
std::vector<std::string> transformed;

std::for_each(songs.begin(), songs.end(), [&](const Song &song) { if (song.containsSample()) transformed.push_back(song.sample); });

然后,包含声音的样本数量就被transformed.size()

这将非常容易实现-另外,无需实现它。 您可以编写函子(又称函数对象)或lambda表达式进行比较并为您保存向量,这将使您可以继续使用std::count_if (或其他标准算法之一)。

std::vector<std::string> mySongs;
std::string myDirectory = "C:/";
std::copy_if(std::begin(samples), std::end(samples), std::back_inserter(mySongs), [](const std::string& s)
{
    // return true or false based on some criteria here
}); 
std::transform(std::begin(mySongs), std::end(mySongs), std::begin(mySongs), [](const std::string& s)
{
    return myDirectory + s;
});

如果我已正确理解,最简单的方法是使用标准算法std :: copy_if。 不需要计数元素,因为您可以简单地通过使用另一个标准函数std :: distance来获取它。 例如,假设您有一个整数数组,并且希望对正值进行计数,并同时将其复制到向量中。 该代码可能如下所示

int a[] = { 1, -3, -5, 9, 2, -4, -1, -7, 5, 8 };

std::vector<int> v;

std::copy_if( std::begin( a ), std::end( a ), 
              std::back_inserter( v ), 
              std::bind2nd( std::greater<int>(), 0 ) );

std::cout << "The number of positive elements is " << std::distance( v.begin(), v.end() ) << std::endl;

这是count_if的实现,您需要做的就是创建一个向量,如果可以接受,则推回结果,并在循环遍历整个范围后返回向量。

暂无
暂无

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

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