C++:获取数组中字符元素的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7875581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: Get index of char element in array
提问by ghostmansd
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'})。我该怎么做?
回答by sehe
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
额外的 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);
}
Bonus C++17 update
额外的 C++17 更新
Here, the original question gets direct support in std::string_view
:
在这里,原始问题得到了直接支持std::string_view
:
#include <string_view>
using namespace std::string_view_literals;
int main() {
return "hello"sv.find('e');
}
回答by Mike Seymour
#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;
}
回答by trojanfoe
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;
}
回答by Firedragon
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;
}
回答by Nim
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...
你很难在性能上击败它......
回答by m0skit0
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;
}