我有这样的方法
template<typename T, typename U>
map<T,U> mapMapValues(map<T,U> old, T (f)(T,U))
{
map<T,U> new;
for(auto it = old.begin(); it != old.end(); ++it)
{
new[it->first] = f(it->first,it->second);
}
return new;
}
Run Code Online (Sandbox Code Playgroud)
而这个想法就是你这样称呼它
BOOST_AUTO_TEST_CASE(MapMapValues_basic)
{
map<int,int> test;
test[1] = 1;
map<int,int> transformedMap = VlcFunctional::mapMapValues(test,
[&](int key, int value) -> int
{
return key + 1;
}
);
}
Run Code Online (Sandbox Code Playgroud)
但是我收到错误:没有函数模板"VlcFunctional :: mapMapValues"匹配参数列表参数类型的实例是:(std :: map,std :: allocator >>,__ lambda1)
知道我做错了什么吗?Visual Studio 2008和Intel C++编译器11.1
我知道我在这里错过了一些简单的东西,但我有一个模板化的成员函数,这是我专门的.
MyClass
{
template<typename T> T GetTFromVariable(shared_ptr<TOtSimpleVariable> v, string s);
}
template<typename T>
T MyClass::GetTFromVariable(shared_ptr<TOtSimpleVariable> v, string s)
{
throw std::runtime_error("Don't know how to convert " + ToString(v->GetString()));
}
template<>
int MyClass::GetTFromVariable<int>(shared_ptr<TOtSimpleVariable> v, string s)
{
return v->GetInteger();
}
template<>
string MyClass::GetTFromVariable<string>(shared_ptr<TOtSimpleVariable> v, string s)
{
return v->GetString();
}
// etc for other specialisations.
Run Code Online (Sandbox Code Playgroud)
这是在我的头文件中定义的(因为模板应该是)但是当我去编译时我得到了一堆多重定义的符号,代表这样的错误是:
OtCustomZenith_logic.lib(PtPathOutput.obj) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall MyClass::GetTFromVariable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class boost::shared_ptr<class TOtSimpleVariable>,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??$GetTFromVariable@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@CommandProperties@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$shared_ptr@VTOtSimpleVariable@@@boost@@V12@@Z) already …Run Code Online (Sandbox Code Playgroud) 大家好,我在成员函数中有以下内容
int tt = 6;
vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt];
set<int>& egressCandidateStops = temp.at(dest);
Run Code Online (Sandbox Code Playgroud)
以及成员变量的以下声明
map<int, vector<set<int>>> m_egressCandidatesByDestAndOtMode;
Run Code Online (Sandbox Code Playgroud)
但编译时遇到错误(英特尔编译器11.0)
1>C:\projects\svn\bdk\Source\ZenithAssignment\src\Iteration\PtBranchAndBoundIterationOriginRunner.cpp(85): error: no operator "[]" matches these operands
1> operand types are: const std::map<int, std::vector<std::set<int, std::less<int>, std::allocator<int>>, std::allocator<std::set<int, std::less<int>, std::allocator<int>>>>, std::less<int>, std::allocator<std::pair<const int, std::vector<std::set<int, std::less<int>, std::allocator<int>>, std::allocator<std::set<int, std::less<int>, std::allocator<int>>>>>>> [ const int ]
1> vector<set<int>>& temp = m_egressCandidatesByDestAndOtMode[tt];
1> ^
Run Code Online (Sandbox Code Playgroud)
我知道它必须是愚蠢的,但我看不出我做错了什么.
UPDATE我从const成员函数调用它,这就是为什么成员变量的类型是const所以我认为类似下面的内容应该修复它:
int dest = 0, tt = 6;
const set<int>& egressCandidateStops = m_egressCandidatesByDestAndOtMode[tt].at(dest);
Run Code Online (Sandbox Code Playgroud)
但没有骰子......仍然是同样的错误.
它似乎来自我的简单测试,但我想知道这是否有保证?
是否存在无法保证订购的条件?
编辑:我特别感兴趣的情况是,如果我填充一个包含大量条目的地图,迭代器的顺序是否会在我的可执行文件的多次运行中相同?如果条目以不同的顺序插入怎么办?
已经阅读过这个问题,我有理由确定使用具有相同输入的浮点算术的给定进程(在相同的硬件上,使用相同的编译器编译)应该是确定性的.我正在研究一个不成立的案例,并试图确定可能导致这种情况的原因.
我已经编译了一个可执行文件,我正在为它提供完全相同的数据,在一台机器上运行(非多线程)但是我得到的错误约为3.814697265625e-06,经过仔细的谷歌搜索,我发现实际上等于1/4 ^ 9 = 1/2 ^ 18 = 1/262144.这非常接近32位浮点数的精度等级(根据维基百科大约7位数)
我怀疑它与已应用于代码的优化有关.我正在使用英特尔C++编译器并将浮点数推测转为快速而非安全或严格.这会使浮点过程不确定吗?是否存在可能导致此行为的其他优化措施?
编辑:根据Pax的建议,我重新编译了浮点投机的代码转向安全,我现在得到稳定的结果.这让我可以澄清这个问题 - 浮点推测实际上做了什么以及如何在应用于完全相同的输入时,导致相同的二进制(即一次编译,多次运行)产生不同的结果?
@Ben我正在使用英特尔(R)C++ 11.0.061 [IA-32]进行编译,而我正在运行英特尔四核处理器.
我想使用std :: vector来控制给定的内存.首先,我很确定这不是一个好习惯,但好奇心对我来说更好,我想知道如何做到这一点.
我遇到的问题是这样的方法:
vector<float> getRow(unsigned long rowIndex)
{
float* row = _m->getRow(rowIndex); // row is now a piece of memory (of a known size) that I control
vector<float> returnValue(row, row+_m->cols()); // construct a new vec from this data
delete [] row; // delete the original memory
return returnValue; // return the new vector
}
Run Code Online (Sandbox Code Playgroud)
_m是一个DLL接口类,它返回一个float数组,调用者负责删除.所以我想将它包装在一个向量中并将其返回给用户....但是这个实现为向量分配新的内存,复制它,然后删除返回的内存,然后返回向量.
我想要做的是直接告诉新的向量它可以完全控制这块内存,所以当它被删除时,内存被清除.
更新:这个的原始动机(从DLL返回的内存)被一些响应者相当牢固地压扁了:)但是,我还是想知道这个问题的答案...有没有办法构建一个std :: vector使用给定的一块预先分配的内存T*数组,以及这个内存的大小?
我有这样的方法
def self.import(file_name, opts = {})
Run Code Online (Sandbox Code Playgroud)
我正试图用YARD记录.然而,这是一种100%副作用的方法(我知道,我知道,副作用,呃!).但对于此方法的用户,实际上没有任何类型的返回对象,但是YARD会生成如下签名:
+ (Object) import(file_name, opts = {})
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉院子导入方法什么都不返回?
我可以告诉它返回零,但这不是真的相同
我已经为字符串转换创建了一个小的实用程序函数,这样我就不必在整个地方创建ostringstream对象了
template<typename T>
inline string ToString(const T& x)
{
std::ostringstream o;
if (!(o << x))
throw BadConversion(string("ToString(") + typeid(x).name() + ")");
return o.str();
}
Run Code Online (Sandbox Code Playgroud)
我想为没有默认重载<< string for stringstream的实例(即std :: pair,std :: set,我自己的类)的实例提供此方法的一些特殊化,并且我遇到了模板的困难.我将用std :: pair示例说明,如果我想能够
string str = ToString(make_pair(3, 4));
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一方法是为int定义显式特化
template<>
inline string ToString(const pair<int,int>& x)
{
std::ostringstream o;
if (!(o << "[" << x.first << "," << x.second << "]"))
throw BadConversion(string("ToString(pair<int,int>)"));
return o.str();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以为通用案例定义这个?
template<>
inline string ToString(const pair<T1,T2>& x)
{
std::ostringstream o;
if (!(o << "[" …Run Code Online (Sandbox Code Playgroud) 我正在使用boost字符串库,并且刚刚遇到了分割方法的简单易用性.
string delimiters = ",";
string str = "string, with, comma, delimited, tokens, \"and delimiters, inside a quote\"";
// If we didn't care about delimiter characters within a quoted section we could us
vector<string> tokens;
boost::split(tokens, str, boost::is_any_of(delimiters));
// gives the wrong result: tokens = {"string", " with", " comma", " delimited", " tokens", "\"and delimiters", " inside a quote\""}
Run Code Online (Sandbox Code Playgroud)
这将是美好而简洁的...但它似乎不适用于引号,而是我必须做类似以下的事情
string delimiters = ",";
string str = "string, with, comma, delimited, tokens, \"and delimiters, inside a quote\"";
vector<string> tokens; …Run Code Online (Sandbox Code Playgroud) 我在编译以下代码段时遇到问题
int temp;
vector<int> origins;
vector<string> originTokens = OTUtils::tokenize(buffer, ","); // buffer is a char[] array
// original loop
BOOST_FOREACH(string s, originTokens)
{
from_string(temp, s);
origins.push_back(temp);
}
// I'd like to use this to replace the above loop
std::transform(originTokens.begin(), originTokens.end(), origins.begin(), boost::bind<int>(&FromString<int>, boost::ref(temp), _1));
Run Code Online (Sandbox Code Playgroud)
所涉及的功能在哪里
// the third parameter should be one of std::hex, std::dec or std::oct
template <class T>
bool FromString(T& t, const std::string& s, std::ios_base& (*f)(std::ios_base&) = std::dec)
{
std::istringstream iss(s);
return !(iss >> f >> t).fail();
} …Run Code Online (Sandbox Code Playgroud)