Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.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++_Arrays - Fatal编程技术网

如何在数组中查找连续元素?C++

如何在数组中查找连续元素?C++,c++,arrays,C++,Arrays,我基本上必须在for循环中创建一个if语句来测试数组中两个位置之间的相等性吗?这里有一个使用我在注释中提到的std::nexture\u find的解决方案 int countconsecutive (const string a[], int n); Return the number of sequences of one or more consecutive identical items in a. string d[9] = { "ben", "chris", "po

我基本上必须在for循环中创建一个if语句来测试数组中两个位置之间的相等性吗?

这里有一个使用我在注释中提到的std::nexture\u find的解决方案

int countconsecutive (const string a[], int n);
Return the number of sequences of one or more consecutive identical items in a.
    string d[9] = {
    "ben", "chris", "polo", "polo", "donald", "donald", "donald", "marco", "marco"
};
    int p = countRuns(d, 9);  //  returns 5


//  The five sequences of consecutive identical items are
       //    "ben"
       //    "chris"
       //    "polo", "polo"
       //    "donald", "donald", "donald"
       //    "marco", "marco"

使用标准算法的真正好处是,在这种情况下,您可以100%免费获得99%的边缘案例处理。这已经适用于空数组和长度为1的数组。此外,如果您想将其更改为其他类型,可以将字符串更改为模板,尽管其中一部分是通过使用auto实现的,但一切仍然可以正常工作。

您可以使用标准算法std::NEXTING\u查找或编写适当的循环

比如说

使用标准算法std::邻接查找


如果您不关心阵列是否被重新洗牌,可以使用std::unique为您完成所有工作:

5
如果没有优雅的boost实现,那么什么答案是完整的呢

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string a[] = 
    {
        "ben", "chris", "polo", "polo", "donald", "donald", "donald", "marco", "marco"
    };


    std::vector<std::string> unique_elements;
    std::unique_copy(a, a + sizeof(a) / sizeof(a[0]), std::back_inserter(unique_elements));
    std::cout << unique_elements.size() << std::endl;

    return 0;
}

对这是正确的。这或使用,auto i=std::nextant\u findbegind,endd@GuyGreer给定的字符串实际上是std::string,或者至少是一个类似的基于值的运算符==绑定到它,也就是说。@decltype\u auto我考虑过提到这个,但决定不提。不过你说得对,有时他必须手动传递一个函子来设置他想要的条件。@GuyGreer:还有一个原因要感谢委员会引入lambda表达式:第一个==a+n的意义是什么?第一:++第一?第一个不能是a+n,否则循环将终止。
#include <iostream>
#include <string>

size_t countconsecutive( const std::string a[], size_t n )
{
    size_t cnt = 0;

    for ( auto first = a; first != a + n; ++first )
    {
        ++cnt;

        for ( auto next = first; ++next != a + n && *first == *next; ++first );  
    }

    return cnt;
}    

int main()
{
    std::string a[] = 
    {
        "ben", "chris", "polo", "polo", "donald", "donald", "donald", "marco", "marco"
    };

    std::cout << countconsecutive( a, sizeof( a ) / sizeof( *a ) ) << std::endl;

    return 0;
}
5
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string a[] = 
    {
        "ben", "chris", "polo", "polo", "donald", "donald", "donald", "marco", "marco"
    };

    std::cout << std::distance(a, std::unique(a, a + sizeof(a) / sizeof(a[0]))) << std::endl;

    return 0;
}
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string a[] = 
    {
        "ben", "chris", "polo", "polo", "donald", "donald", "donald", "marco", "marco"
    };


    std::vector<std::string> unique_elements;
    std::unique_copy(a, a + sizeof(a) / sizeof(a[0]), std::back_inserter(unique_elements));
    std::cout << unique_elements.size() << std::endl;

    return 0;
}
#include <boost/range/adaptor/adjacent_filtered.hpp>
#include <boost/range/size.hpp>
#include <iostream>

int main(int argc, const char* argv[])
{
    std::string a[] = 
    {
        "ben", "chris", "polo", "polo", "donald", "donald", "donald", "marco", "marco"
    };


    std::cout << boost::size(a | boost::adaptors::adjacent_filtered(std::not_equal_to<std::string>()));

    return 0;
}