C ++:获取数组中char元素的索引

时间:2022-08-21 23:55:15

I need to get number of character in array.

我需要获取数组中的字符数。

const char myarray[5] = {'0', 'a', 'e', 'f', 'c'}; // Create array of char
int number=0; // Create variable
number = getposition(myarray, 'f'); // Now number equals to 3
number = getposition(myarray, 'z'); // -1, because array doesn't have this char

My task is easy because array don't have repeating characters (for example, it can't be smth like this: {'a', '1', 'f', 'a'}). How can I do it?

我的任务很简单,因为数组没有重复的字符(例如,它不能像这样:{'a','1','f','a'})。我该怎么做?

6 个解决方案

#1


14  

A little bit more C++:

多一点C ++:

 #include <algorithm>

int getposition(const char *array, size_t size, char c)
{
     const char* end = array + size;
     const char* match = std::find(array, end, c);
     return (end == match)? -1 : (match-array);
}

A lot more C++:

更多C ++:

template <typename T, size_t N>
int getposition(const T (&array)[N], const T c)
{
     const T* match = std::find(array, array+N, c);
     return (array+N==match)? -1 : std::distance(array, match);
}

Bonus C++11/C++11 update

#include <algorithm>
#include <iterator>

template <typename Range, typename T>
size_t index_of(Range const& range, T const& c) {
    using std::begin;
    using std::end;

    auto b = begin(range), e = end(range);
    auto match = std::find(b, e, c);

    return (e==match)? -1 : std::distance(b, match);
}

Bonus C++17 update

Here, the original question gets direct support in std::string_view:

在这里,原始问题在std :: string_view中获得直接支持:

Live On Coliru

住在科利鲁

#include <string_view>
using namespace std::string_view_literals;

int main() {
    return "hello"sv.find('e');
}

#2


8  

#include <algorithm>

template <typename T, size_t size>
int getposition(T const (&array)[size], T const & c)
{
    T const * found = std::find(&array[0], &array[size], c);
    return found == &array[size] ? -1 : found - array;
}

#3


4  

You need to tell the getposition() method how many elements to search within the array and as the array is initialised at compile time you can use the sizeof directive:

您需要告诉getposition()方法在数组中搜索多少个元素,并且在编译时初始化数组,您可以使用sizeof指令:

int number = getposition(myarray, sizeof(myarray), 'f');

...

int getposition(const char *array, size_t size, char c)
{
    for (size_t i = 0; i < size; i++)
    {
        if (array[i] == c)
            return (int)i;
    }
    return -1;
}

#4


1  

int getposition(const char* a, int arr_size, char to_find)
{
    int pos = -1;

    for(int i = 0; i < arr_size; ++i)
    {
        if(a[i] == to_find)
        {
            pos = i;
            break;
        }
    }

    return pos;
}

#5


0  

If this really is a pure decoding exercise - why not re-organize your array... then lookup is constant time - e.g..

如果这真的是一个纯粹的解码练习 - 为什么不重新组织你的数组...然后查找是恒定时间 - 例如..

int lt[128]; // ignoring negative values..

memset(lt, -1, 128); // initialize all to -1

// set the ones you want mappings for..
lt['0'] = 0;
lt['a'] = 1;
lt['e'] = 2;
lt['f'] = 3;
lt['c'] = 4;

so now your look up function is:

所以现在你的查找功能是:

int indexOf(char v) { return lt[v]; }

You'd be hard-pressed to beat that for performance...

为了表现,你很难击败它...

#6


-2  

You need to pass the array size as well to the function.

您还需要将数组大小传递给函数。

int getposition(const char* array, size_t array_size, char value)
{
    int ret = -1;

    int i = 0;
    bool found = false;
    while (i < array_size && !found)
    {
        found = (array[i++] == value);
    }

    if (found)
    {
        ret = i - 1;
    }

    return ret;
}

#1


14  

A little bit more C++:

多一点C ++:

 #include <algorithm>

int getposition(const char *array, size_t size, char c)
{
     const char* end = array + size;
     const char* match = std::find(array, end, c);
     return (end == match)? -1 : (match-array);
}

A lot more C++:

更多C ++:

template <typename T, size_t N>
int getposition(const T (&array)[N], const T c)
{
     const T* match = std::find(array, array+N, c);
     return (array+N==match)? -1 : std::distance(array, match);
}

Bonus C++11/C++11 update

#include <algorithm>
#include <iterator>

template <typename Range, typename T>
size_t index_of(Range const& range, T const& c) {
    using std::begin;
    using std::end;

    auto b = begin(range), e = end(range);
    auto match = std::find(b, e, c);

    return (e==match)? -1 : std::distance(b, match);
}

Bonus C++17 update

Here, the original question gets direct support in std::string_view:

在这里,原始问题在std :: string_view中获得直接支持:

Live On Coliru

住在科利鲁

#include <string_view>
using namespace std::string_view_literals;

int main() {
    return "hello"sv.find('e');
}

#2


8  

#include <algorithm>

template <typename T, size_t size>
int getposition(T const (&array)[size], T const & c)
{
    T const * found = std::find(&array[0], &array[size], c);
    return found == &array[size] ? -1 : found - array;
}

#3


4  

You need to tell the getposition() method how many elements to search within the array and as the array is initialised at compile time you can use the sizeof directive:

您需要告诉getposition()方法在数组中搜索多少个元素,并且在编译时初始化数组,您可以使用sizeof指令:

int number = getposition(myarray, sizeof(myarray), 'f');

...

int getposition(const char *array, size_t size, char c)
{
    for (size_t i = 0; i < size; i++)
    {
        if (array[i] == c)
            return (int)i;
    }
    return -1;
}

#4


1  

int getposition(const char* a, int arr_size, char to_find)
{
    int pos = -1;

    for(int i = 0; i < arr_size; ++i)
    {
        if(a[i] == to_find)
        {
            pos = i;
            break;
        }
    }

    return pos;
}

#5


0  

If this really is a pure decoding exercise - why not re-organize your array... then lookup is constant time - e.g..

如果这真的是一个纯粹的解码练习 - 为什么不重新组织你的数组...然后查找是恒定时间 - 例如..

int lt[128]; // ignoring negative values..

memset(lt, -1, 128); // initialize all to -1

// set the ones you want mappings for..
lt['0'] = 0;
lt['a'] = 1;
lt['e'] = 2;
lt['f'] = 3;
lt['c'] = 4;

so now your look up function is:

所以现在你的查找功能是:

int indexOf(char v) { return lt[v]; }

You'd be hard-pressed to beat that for performance...

为了表现,你很难击败它...

#6


-2  

You need to pass the array size as well to the function.

您还需要将数组大小传递给函数。

int getposition(const char* array, size_t array_size, char value)
{
    int ret = -1;

    int i = 0;
    bool found = false;
    while (i < array_size && !found)
    {
        found = (array[i++] == value);
    }

    if (found)
    {
        ret = i - 1;
    }

    return ret;
}