查找索引值在char表中

lookup index value in a char table

本文关键字:表中 char 索引值 查找      更新时间:2023-10-16

我试图通过浏览数组来找到索引号,但我无法使用循环。是否可以?

我已经附加了代码以使用循环进行操作。

#include <iostream>
using namespace std;
const char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
'A', 'B', 'C', 'D', 'E', 'F', };
int find_index(string str);
int main()
{
    string str = "A";
    cout << find_index(str) << endl;
}
int find_index(string str)
{
    // using a loop, in this case for loop.
    for (int i = 0 ; i < 16 ; i++)
    {
        // cout << digits[i] << endl;
        if ( digits[i] == str[0] )
        {
            return i;
        }
    }
    return -1;
}

可以在没有循环的情况下做同样的事情?

最好将数组替换为字符串文字

const char *digits = { "0123456789ABCDEF" };

并使用标准C函数strchr。例如

size_t find_index( char c )
{
    const char *digits = { "0123456789ABCDEF" };
    c = std::toupper( ( unsigned char )c );
    const char *p = std::strchr( digits, c );
    return p == nullptr ? -1 : p - digits;   
}

这是一个指示的程序。

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
size_t find_index( char c )
{
    const char *digits = { "0123456789ABCDEF" };
    c = std::toupper( ( unsigned char )c );
    const char *p = std::strchr( digits, c );
    return p == nullptr ? -1 : p - digits;   
}
int main()
{
    std::string s( "0123456789abcdef" );
    for ( char c : s )
    {
        size_t i = find_index( c );
        if ( i == size_t( -1 ) )
        {
            std::cout << "c is not a valid digitn";
        }
        else
        {
            std::cout << c << ": " << i << 'n';
        }
    }
}     

程序输出是

0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
6: 6
7: 7
8: 8
9: 9
a: 10
b: 11
c: 12
d: 13
e: 14
f: 15

更严格的函数实现(不包括字符''(可以看起来以下方式

size_t find_index( char c )
{
    const char *digits = { "0123456789ABCDEF" };
    const char *p = nullptr;    
    if ( c != '' )
    {
        c = std::toupper( ( unsigned char )c );
        p = std::strchr( digits, c );
    }
    return p == nullptr ? -1 : p - digits;   
}

另一种方法是使用标准算法。

例如

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
size_t find_index( char c )
{
    static const char digits[] = 
    { 
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'A', 'B', 'C', 'D', 'E', 'F', 
    };
    auto it = std::find( std::begin( digits ), std::end( digits ), std::toupper( ( unsigned char )c ) );
    return it == std::end( digits ) ? -1 : std::distance( std::begin( digits ), it );   
}
int main()
{
    std::string s( "0123456789abcdef" );
    for ( char c : s )
    {
        size_t i = find_index( c );
        if ( i == size_t( -1 ) )
        {
            std::cout << "c is not a valid digitn";
        }
        else
        {
            std::cout << c << ": " << i << 'n';
        }
    }
}     

输出将与上面显示的相同。

作为对数组的排序,您也可以使用标准算法std::lower_bound而不是std::find用于ASCII字符(不适合EBCDIC(。

if (loopsAllowed == false) {
    useRecursion();
}
else {
    useLoops();
}

如果您对学校项目有奇怪的要求,这是一种非常标准的做事方式。我要避免的一件事是使用goto跳到代码的先前部分。这实际上是循环的。

另外,给定足够的堆栈存储器,在技术上可以使用循环的任何东西。