我正在调试一些旧的C代码,它有一个定义#define PI 3.14 ...其中...大约是50个其他数字.
为什么是这样?我说我可以将数字减少到大约16位小数,但是我的老板对我咆哮说其他数字用于平台独立性和向前兼容性.但是程序会慢下来吗?
考虑这三个陈述:
std::map<int, std::string> foo;
std::map<int, std::string>::value_type;
decltype(foo)::value_type;
Run Code Online (Sandbox Code Playgroud)
为什么最后一个合法?我认为这decltype(foo)将是一个运算符,产生std::map<int, std::string>我可以从中提取的地图类型value_type.
我正在使用MSVC2012.
众所周知,你可以写
int a;
int b;
b = a = 3;
Run Code Online (Sandbox Code Playgroud)
但我想写
foo f;
bar b;
f = b = null;
Run Code Online (Sandbox Code Playgroud)
这在语法上不合法.大概是因为null在分配时有一个与之关联的b类型,f即使引用的值是,也无法分配该类型null.这是正确的,除了显而易见之外还有其他选择f = null; b = null;吗?
我有一个std::map<std::string, std::vector<double>> foo,我使用一个函数慢慢建立的内容
bar(
std::string key /*key name*/,
std::size_t index /*element index of the vector*/,
double value /*value to put in the vector at that index*/
)
Run Code Online (Sandbox Code Playgroud)
这个函数foo以一种我信任的方式填充,从函数参数类型和那些简短的注释中可以看出.
但是,我担心当我添加一个新密钥时会发生这种情况
auto it = foo.find(index);
if (it == foo.end()){
/*ToDo - avoid taking a vector value copy*/
Run Code Online (Sandbox Code Playgroud)
我最终创建了一个新的std::vector<double>,并把它放在地图中.这导致我获取该向量的值副本.有没有办法(除了使用智能指针作为地图值)这样做而不需要复制值?
由于一些静态数据,我有一个功能
void foo(MyNamespace::bar)
在某个编译单元中定义.但它的使用点在于另一个编译单元.所以我用
namespace MyNamespace
{
extern void foo(bar);
}
Run Code Online (Sandbox Code Playgroud)
但是链接器找不到函数定义.我在滥用extern吗?
假设我有班级
template<
typename T1, /*this is probably an integral type*/
T1 Default /*this is a typical value of that integral type*/
> class Foo {};
Run Code Online (Sandbox Code Playgroud)
而这对于一个给定的实例T1和Default,说foo.
我可以decltype(foo)用来获得完整的类型.
我可以使用一些语法来获取值Default吗?
我有以下代码,它返回双向count量向量中最后一个元素的总和foo:
return std::accumulate(foo.rbegin().base() - std::min(count, foo.size()), foo.rbegin().base(), 0);
Run Code Online (Sandbox Code Playgroud)
但它忽略了任何小数部分.为什么?
为了将字符串拆分为向量,我使用
std::vector<std::string> v;
boost::split(v, input, boost::is_any_of("|"));
Run Code Online (Sandbox Code Playgroud)
Boost或STL中是否存在执行此操作相反功能的函数,即形式为join的函数
join(v, output, "|")
Run Code Online (Sandbox Code Playgroud) 我有一个静态方法的类.
在那个静态方法中,我需要包含该方法的类的规范名称.(我宁愿不硬编码).
一种显而易见的方法是使用,this.getClass().getCanonicalName();但是,当然,我不能在static函数中执行此操作,因为this它没有意义.
有没有办法做到这一点?
我正在使用std::find_if并且作为第三个参数我想在其中使用三元组std::bind2nd:
std::bind2nd(foo ? std::greater<K>() : std::greater_equal<K>(), bar);
Run Code Online (Sandbox Code Playgroud)
对于某种类型K.foo是bool一面旗帜.但是,这不是因为syntatically正确的std::greater<K>(),并std::greater_equal<K>()有不同的类型.
理想情况下,我想删除三元(因为上面的循环运行各种bars)并且有
auto predicate = foo ? std::greater<K>() : std::greater_equal<K>();
std::bind2nd(predicate, bar); /*this statement is in a loop*/
Run Code Online (Sandbox Code Playgroud)
但这不合法,因为predicate在编译时不知道类型.但是std::bind2nd可以采取任何一种类型,所以我的想法是可以有一种方法来实现我想要的.在那儿?