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

发布于 2024-12-12 01:12:47 字数 363 浏览 0 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

百思不得你姐 2024-12-19 01:12:47

多一点 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);
}

多一点 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);
}

额外的 C++11/C++11 更新

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

额外的 C++17 更新

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

<强>直播科利鲁

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

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

A little bit more 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++:

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:

Live On Coliru

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

int main() {
    return "hello"sv.find('e');
}
只是偏爱你 2024-12-19 01:12:47
#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;
}
#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;
}
三人与歌 2024-12-19 01:12:47

您需要告诉 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;
}

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:

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;
}
段念尘 2024-12-19 01:12:47
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;
}
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;
}
揽月 2024-12-19 01:12:47

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

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;

所以现在你的查找函数是:

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

你很难在性能上击败它......

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...

油饼 2024-12-19 01:12:47

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

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文