- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
头文件(test.h):
#ifndef _test_h_
#define _test_h_
#include <string>
#include <sstream>
#include <map>
typedef std::map<int, std::string> MIS;
//-----1-----
template<typename T> const std::pair<int, std::string> mkpair1(T t)
{
static int i=0;
std::stringstream ss;
ss << t;
return std::pair<int, std::string>(++i, ss.str());
}
template<typename...Any> void mkmap1(MIS &m, Any...any)
{
m = { mkpair1(any)... };
};
//-----2-----
template<typename T> const std::pair<int, std::string> mkpair2(int i, T t)
{
std::stringstream ss;
ss << t;
return std::pair<int, std::string>(i, ss.str());
}
template<typename...Any> void mkmap2(MIS &m, Any...any)
{
static int i=0;
m = { mkpair2(++i, any)... };
};
//-----3-----
template<typename T> const std::pair<int, std::string> mkpair3(int &i, T t)
{
std::stringstream ss;
ss << t;
return std::pair<int, std::string>(++i, ss.str());
}
template<typename...Any> void mkmap3(MIS &m, Any...any)
{
int i=0;
m = { mkpair3(i, any)... };
};
#endif
程序文件:
#include <iostream>
#include "test.h"
void ShowM(int x, const MIS &m)
{
std::cout << "\n---" << x << "---\n";
for (auto p:m) std::cout << p.first << " -> " << p.second << "\n";
}
int main(int argc, const char *argv[])
{
MIS m;
m.clear();
mkmap1(m, 1, "two", 3.1415, "four", 5);
ShowM(1, m);
m.clear();
mkmap2(m, 1, "two", 3.1415, "four", 5);
ShowM(2, m);
m.clear();
mkmap3(m, 1, "two", 3.1415, "four", 5);
ShowM(3, m);
return 0;
}
输出:
---1---
1 -> 1
2 -> four
---2---
1 -> 1
2 -> two
3 -> 3.1415
4 -> four
5 -> 5
---3---
1 -> 1
2 -> two
3 -> 3.1415
4 -> four
5 -> 5
为什么第一个模板函数无法正确构建 map ?结果很奇怪。 (在 Linux 上使用 gcc)
编辑:
根据“101010”的简洁解释,来自 cplusplus.com :
Because element keys in a map are unique, the insertion operation checks whether each inserted element has a key equivalent to the one of an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value).
最佳答案
正确@immibis评论中提到,由于模板参数推导,模板函数 mkpair1
生成了 3 个模板实例化,即:
mkpair1<int>(...) // for inputs 1, 5
mkpair1<double>(...) // for input 3.1415
mkpair1<const char*>(...) // for input "tow" and "four"
因此,可变模板扩展看起来像这样:
m = { mkpair1<int>(1),
mkpair1<const char*>("two"),
mkpair1<double>(3.1415),
mkpair1<const char*>("four"),
mkpair1<int>(5)
};
现在,由于对于生成的每个模板实例,您还定义了不同的 static int i
,因此您将具有以下插入方案:
m.insert(std::make_pair(1, "1"); // OK
m.insert(std::make_pair(1, "two"); // ignored due to key 1 already exists
m.insert(std::make_pair(1, "3.1415"); // ignored due to key 1 already exists
m.insert(std::make_pair(2, "four"); // OK
m.insert(std::make_pair(2, "5")); // ignored due to key 2 already exists
因此,你得到的结果是正确的:
---1---
1 -> 1
2 -> four
关于c++ - 模板函数中的静态变量失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34826247/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!