查找索引值在char表中
lookup index value in a char table
我试图通过浏览数组来找到索引号,但我无法使用循环。是否可以?
我已经附加了代码以使用循环进行操作。
#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
跳到代码的先前部分。这实际上是循环的。
另外,给定足够的堆栈存储器,在技术上可以使用循环的任何东西。
相关文章:
- 反向给定链表中的K节点
- 我们可以删除链表中静态内存中的节点吗
- C++,指针数组,指向双链表中的条目
- 链表中写入访问冲突的未知原因
- 为什么C中的通用链表中存储的数据已损坏
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 如何在 sqlite3 中的表中添加整数列表
- 链表中的显示功能无法正常工作
- 链表中的 C++ 析构函数
- 编写一个函数来删除单链表中的节点(尾部除外),仅授予对该节点的访问权限
- 如何在 c++ 中比较 2 个链表并将匹配的数据放入另一个链表中
- 在链链表中手动插入第五个元素
- 在链表中的第 n 位插入显示分割错误
- 如何在C++中移动字母表中的字母?
- 将数组的元素添加到链表中
- 有没有办法将 vector<unsigned char> 插入 postgresql 表中,具有 bytea 属性,没有 UTF8 编码错误?
- 查找索引值在char表中
- C++添加到包含 char[] 作为参数的链表中
- 如何将char表中的单个炭转换为int
- 将十六进制数的 char[] 转换为对应于 ASCII 表中十六进制数字的字母的 char[] 并反转