高效存储多达 2048 个字符的数组
Efficiently store array of up to 2048 characters?
从另一个源获取输入;填充最多 2048 个字符的字符串。
填充和比较此字符串的最有效方法是什么? - 我也希望能够轻松地附加到字符串中。
以下是我的三次尝试:
C型版本
#include <cstdio>
#include <cstring>
int main(void) {
char foo[2048];
foo[0]='a', foo[1]='b', foo[2]='c', foo[3]=' '; // E.g.: taken from u-input
puts(strcmp(foo, "bar")? "false": "true");
}
C++式版本 0
#include <iostream>
int main() {
std::string foo;
foo.reserve(2048);
foo += "abc"; // E.g.: taken from user-input
std::cout << std::boolalpha << (foo=="bar");
}
C++式版本 1
#include <iostream>
int main() {
std::string foo;
foo += "abc"; // E.g.: taken from user-input
std::cout << std::boolalpha << (foo=="bar");
}
最有效的方法取决于您的优化对象。
一些常见的标准:
- 程序速度
- 程序大小
- 工作集大小
- 代码大小
- 程序员时间
- 安全
毫无疑问,1 和 2 的国王,在您的示例中可能也是 3,是 C 风格。
对于 4 和 5,C++样式 1。
第 6 点可能是C++式的。
需要适当地组合强调这些目标,恕我直言,这有利于选项 0 C++。
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- 字符数组中的元素数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用字符数组作为 Map 中的键
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 按顺序声明的字符数组重叠
- 在 C++ 中字符串的大小与字符数组的大小
- 寻找一种更好的方法来表示无符号字符数组
- 如何打印 2D 字符数组C++
- 从字符数组的元素中减去'a'是什么意思
- 我是否不正确地集中了这些字符数组?