我在标题文件中有以下代码.
#pragma once
class error_code {
public:
unsigned __int64 hi;
unsigned __int64 lo;
};
std::ostream& operator<< (std::ostream& o, const error_code& e) {
return o << "[" << e.hi << "," << e.lo << "]";
}
Run Code Online (Sandbox Code Playgroud)
我得到链接错误,当项目中有2个cpp包含此头文件时.
错误LNK2005:"类error_code __cdecl运算符|(类error_code const&,类ViTrox :: error_code const&)"(?? U @@ YA?AVerror_code @ 0 @ ABV10 @ 0 @ Z)已在xxx.obj中定义
我知道如果我将定义移动operator<<到cpp文件或DLL文件,我可以解决这个问题.
但是,我只想将它们放在SINGLE头文件中.有没有什么技术可以做到这一点?或者我必须将定义分离到另一个文件?
如何从std::set?中有效地选择随机元素?
一个std::set::iterator是不是一个随机访问迭代.所以我不能直接索引随机选择的元素,就像我可以用于std::deque或std::vector
我可以从中返回迭代器std::set::begin()并将其递增0到std::set::size()-1时间,但这似乎做了很多不必要的工作.对于接近集合大小的"索引",我最终将遍历树的整个前半部分,即使已经知道该元素将不会在那里找到.
有更好的方法吗?
在效率的名义上,我愿意将"随机"定义为比我可能用于在向量中选择随机索引的任何方法更少随机.称之为"合理随机".
编辑...
下面有很多有见地的答案.
简短版本是即使您可以在log(n)时间内找到特定元素,也无法通过界面在该时间内找到任意元素.std::set
正如我解释它,MSDN的定义的numeric_limits::is_exact几乎都是假的:
在[this]类型上完成的[all]计算没有舍入误差.
而IBM的定义几乎总是正确的:(或圆形的定义,这取决于你怎么看它)
一种对其所有值都有精确表示的类型
我确定的是,我可以2在a double和a中存储一个,long而且它们都可以完全代表.
然后,我可以将它们分开,10并且两者都不会完全保持数学结果.
给定任何数值数据类型T,定义的正确方法是std::numeric_limits<T>::is_exact什么?
编辑:我已经从许多答案中提供的详细信息中发布了我认为对此问题的准确答案. 这个答案不是赏金的竞争者.
看完ostream::operator <<c ++参考后,
我注意到以下声明:
ostream& operator<< (bool val);
ostream& operator<< (short val);
ostream& operator<< (unsigned short val);
ostream& operator<< (int val);
ostream& operator<< (unsigned int val);
ostream& operator<< (long val);
ostream& operator<< (unsigned long val);
ostream& operator<< (float val);
ostream& operator<< (double val);
ostream& operator<< (long double val);
ostream& operator<< (void* val);
ostream& operator<< (streambuf* sb );
ostream& operator<< (ostream& (*pf)(ostream&));
ostream& operator<< (ios& (*pf)(ios&));
ostream& operator<< (ios_base& (*pf)(ios_base&));
Run Code Online (Sandbox Code Playgroud)
但后来我发现还有以下声明:
ostream& operator<< (ostream& os, char c);
ostream& operator<< …Run Code Online (Sandbox Code Playgroud) 从C++标准:
5.2.10.3
reinterpret_cast执行的映射可能会或可能不会产生与原始值不同的表示.
我已经在这个网站接受过培训,相信并重复这一点. (即使它可能只是琐事).甲reinterpret_cast从float*到int*被允许产生不同的位模式.唯一的保证是 - reinterpret_cast返回结果float*将产生原始位模式.
我的问题:这会发生吗?是否有一个现有的,真实的平台或CPU或编译器实际上reinterpret_cast是一个不同的位模式?如果不是,是否有任何地方现实情况reinterpret_cast有任何运行时开销?
根据我的经验reinterpret_cast,演员是编译器的指令,而不是运行时.
从[class.access]/7我们得到以下句子:
类似地,
A::B作为基本说明符的使用是良好形成的,因为它D是派生自的A,因此必须推迟对基本说明符 s的检查,直到看到整个基本说明符列表.
class A {
protected:
struct B { };
};
struct D: A::B, A { };
Run Code Online (Sandbox Code Playgroud)
查看clang的实例.事实上,clang还抱怨这个片段,不需要延期.
class A {
protected:
struct B { };
};
struct D: A, A::B { };
Run Code Online (Sandbox Code Playgroud)
为什么这段代码不能编译?
PS:gcc和VS21013也不编译代码.
在C++中是否有这样的宏(交叉编译器或特定于编译器):
#if isclass(NameSpace::MyClass)
Run Code Online (Sandbox Code Playgroud)
会有用的.
我有一个C++类" X",如果将它们的容器发送到一个容器,它将具有特殊意义std::ostream.
我最初专门用于std::vector<X>:
std::ostream& operator << ( std::ostream &os, const std::vector<X> &c )
{
// The specialized logic here expects c to be a "container" in simple
// terms - only that c.begin() and c.end() return input iterators to X
}
Run Code Online (Sandbox Code Playgroud)
如果我想支持std::ostream << std::deque<X>或std::ostream << std::set<X>任何类似的容器类型,我所知道的唯一解决方案是复制粘贴整个函数并仅更改函数签名!
有没有办法一般编码operator << ( std::ostream &, const Container & )?
(" Container"这里将是满足上述注释说明的任何类型.)
我有两个.csv文件,其中文件1中的第一行是:
MPID,Title,Description,Model,Category ID,Category Description,Subcategory ID,Subcategory Description,Manufacturer ID,Manufacturer Description,URL,Manufacturer (Brand) URL,Image URL,AR Price,Price,Ship Price,Stock,Condition
Run Code Online (Sandbox Code Playgroud)
文件2的第一行:
Regular Price,Sale Price,Manufacturer Name,Model Number,Retailer Category,Buy URL,Product Name,Availability,Shipping Cost,Condition,MPID,Image URL,UPC,Description
Run Code Online (Sandbox Code Playgroud)
然后每个文件的其余部分都填充了信息.
如您所见,两个文件都有一个名为MPID的公共字段(文件1:col 1,文件2:col 9,其中第一个col为col 1).
我想创建一个新文件,通过查看这个列来组合这两个文件(如:如果两个文件中都有一个MPID,那么在新文件中,这个MPID将出现在文件1的两行中)和它在文件2中的行.如果一个MPID只出现在一个文件中,那么它也应该进入这个组合文件.
文件未以任何方式排序.
如何在带有shell脚本或python的debian机器上执行此操作?
谢谢.
编辑:两个文件除了分隔字段之外没有逗号.
在给定的c代码片段中,该行的等效代码是
int *count = (int *)calloc(sizeof(int), 256);什么?
int *getCharCountArray(char *str)
{
int *count = (int *)calloc(sizeof(int), 256);
int i;
for (i = 0; *(str+i); i++)
count[*(str+i)]++;
return count;
}
Run Code Online (Sandbox Code Playgroud)
是否可以在不使用calloc的情况下执行此操作?我们如何在c ++中使用malloc和new声明这个?