- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想要一个存储一对不同类型变量的类,但我需要将变量的零或空默认值作为模板参数传递。我可以为 int 或 double 这样做,但是我如何为 string 这样做呢?我知道 c++ 目前没有字符串参数,但有哪些替代设计。我需要这样的东西:
#include <iostream>
#include <string>
using namespace std;
template <typename atype, typename btype, atype anull, btype bnull>
class simpleClass {
public:
atype var1;
btype var2;
simpleClass<atype, btype, anull, bnull> *parent; // pointer to parent node
simpleClass(); ~simpleClass();
};
template <typename atype, typename btype, atype anull, btype bnull>
simpleClass<atype, btype, anull, bnull>::simpleClass() { var1 = anull; var2 = bnull;
parent = NULL; }
template <typename atype, typename btype, atype anull, btype bnull>
simpleClass<atype, btype, anull, bnull>::~simpleClass() {}
int main() {
simpleClass<string, int, "", 0> obj;
obj.var1 = "hello";
obj.var2 = 45;
cout << obj.var2;
return 0;
}
编译这个,我得到
error: ‘struct std::string’ is not a valid type for a template constant parameter
最佳答案
您不能将非整数类型作为模板参数传递,指针和引用除外。您可能希望的最佳行为是传递一个函数,该函数返回 atype
和 btype
的“默认”值。
关于c++ - 需要将空字符串作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6420709/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!