C++ 替换循环以检索数组中第一个元素的索引

C++ 替换循环以检索数组中第一个元素的索引,c++,stl,C++,Stl,是否有更好的方法使用谓词搜索第一个元素的索引 // ... this code looks a bit long to me. Anyway to do better? auto it = std::find_if(collection + startAt, collection + COLLECTION_SIZE, [](const char* line) { return strlen(line

是否有更好的方法使用谓词搜索第一个元素的索引

// ... this code looks a bit long to me.  Anyway to do better?
auto it = std::find_if(collection + startAt, 
                       collection + COLLECTION_SIZE, 
                       [](const char* line) { return strlen(line) <= 10; });
int idx = std::dist(collection, it); //= it - collection;
/。。。我觉得这个代码有点长。无论如何,你想做得更好吗?
auto it=std::find_if(collection+startAt,
集合+集合大小,
[](常量字符*行){return strlen(行)10;posEmptyItem++}

STD::那么,你想要的到底是什么?考虑使用而不是减去你的迭代器。它适用于非随机存取迭代器。感谢STD::距离。表达式IDX=它对我来说看起来很奇怪。如果你正在寻找工作代码的反馈,你应该代替它。<代码> char *集合[收藏尺寸]…<代码>不是标准C++,应该从编译器那里接收到错误或警告。如果您想要一个指向字符串文字的指针,那么您应该使用<代码> const char */COD>。
for (posEmptyItem = startAt; strlen(collection[posEmptyItem]) > 10; posEmptyItem++) {}
std::cout << posEmptyItem << std::endl;
#include "stdafx.h"
#include <cstring>
#include <iostream>
#include <algorithm>

#define COLLECTION_SIZE 123

int main()
{
    char* collection[COLLECTION_SIZE]{ "time11,time2,time3",
                                       "time12,time2,time3",
                                       "time13,time2,time3",
                                       "time14,time2,time3",
                                       "time15,time2,time3",
                                       "x\n", 
                                       "" };
    auto startAt = 2;
    int posEmptyItem;

    // legacy code
    for (posEmptyItem = startAt; strlen(collection[posEmptyItem]) > 10; posEmptyItem++) {}
    std::cout << posEmptyItem << std::endl;

    // replace the loop to search an index by calling to standard library
    auto it = std::find_if(collection + startAt, 
                           collection + COLLECTION_SIZE, 
                           [](const char* line) { return strlen(line) <= 10; });
    posEmptyItem = it - collection;
    std::cout << posEmptyItem << std::endl;

    return 0;
}