我需要在文件中做一堆单词替换,并希望用vi命令,而不是像EX这样的EX命令:%s///g.我知道这是在当前光标位置替换单词的典型方式:cw<text><esc>但是有没有办法用未命名寄存器的内容作为替换文本并且不覆盖寄存器?
可以创建一个通过构造函数参数初始化的匿名对象,例如在下面的return语句中.
struct S {
S(int i_, int j_) : i(i_), j(j_) { }
int i, j;
};
S f()
{
return S(52, 100);
}
int main()
{
cout << f().i << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,是否可以类似地创建一个使用大括号初始化程序初始化的匿名聚合?例如,可以将f()的主体折叠到下面,直到单个return语句而没有"s"吗?
struct S {
int i, j;
};
S f()
{
S s = { 52, 100 };
return s;
}
int main()
{
cout << f().i << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 为什么在此代码中必须使用Child传递构造函数?我认为它不会,但编译器(gcc和VS2010)在我删除它时会抱怨.有优雅的解决方法吗?将这个垫片插入子类似乎毫无意义.
class Parent
{
public:
Parent(int i) { }
};
class Child : public Parent
{
public:
Child(int i) : Parent(i) { }
};
int main()
{
Child child(4);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 鉴于int a;,我知道以下内容返回a可容纳的最大值.
numeric_limits<int>::max()
不过,我想获得相同的信息不知道a是int.我想做这样的事情:
numeric_limits<typeof<a>>::max()
没有这种确切的语法,但使用ISO C++甚至可能吗?
type_of()是最接近的,但我宁愿不在我们的代码库中添加任何额外内容.由于我们已经使用了Boost,ÉricMalenfant对Boost.Typeof的引用促使我使用
numeric_limits<BOOST_TYPEOF(m_focusspeed)>::max()
我以前从没用过它.再次感谢您提供了许多明智的回复.
以下玩具程序将一种音乐转换为相应的颜色.它编译并执行正常 - COUNTRY失败转换,如预期的那样,conversion()函数返回默认值,WHITE.但是,如果我删除模板参数,<MUSIC, COLOR>模板参数推断无法识别要使用的类型.我怎样才能获得演绎?
#include <map>
#include <iostream>
#include "boost/assign.hpp"
template<typename Key, typename T>
T convert(const Key &k, const T &d, const std::map<Key, T> &m) {
typename std::map<Key, T>::const_iterator it = m.find(k);
return it == m.end() ? d : it->second;
}
enum MUSIC { ROCK, RAP, EDM, COUNTRY };
enum COLOR { RED, BLUE, ORANGE, WHITE };
int main()
{
COLOR c = convert<MUSIC, COLOR>(COUNTRY, WHITE,
boost::assign::map_list_of (RAP, RED) (EDM, BLUE) (ROCK, RED)); …Run Code Online (Sandbox Code Playgroud) 当我遍历a时vector<bool>,我发现通过迭代器解除引用的元素被识别为它们是const.这是为什么?更改容器或元素类型,例如list<bool>或vector<short>,并且元素是非const.这段代码显示了我在说什么:
typedef bool T;
#define C vector
istringstream &operator>>(istringstream &iss, T &v)
{
cout << "non-const" << endl;
return iss;
}
istringstream &operator>>(istringstream &iss, const T &v)
{
cout << "const" << endl;
return iss;
}
istringstream &operator>>(istringstream &iss, C<T> &c)
{
for (C<T>::iterator it = c.begin(); it != c.end(); ++it)
{
iss >> *it;
}
return iss;
}
int main()
{
C<T> c(1);
istringstream iss("1");
iss >> c;
}
Run Code Online (Sandbox Code Playgroud)
因为vector<bool>,该程序将"const"打印到控制台.将顶部的typedef和manifest常量更改为除了这两个组合之外的任何内容,并打印"non-const".此外,如果我替换行,iss …
我希望operator>>()任何"基础"类型和任何容器类型都有重载.这是我到目前为止:
typedef uintmax_t my_t;
template <typename T>
std::istringstream &operator>>(std::istringstream &iss, T &v)
{
static my_t um = 6009;
v = um++;
return iss;
}
template <template <class> class C, typename T>
std::istringstream &operator>>(std::istringstream &iss, C<T> &c)
{
for (typename C<T>::iterator it = c.begin(); it != c.end(); ++it)
iss >> *it;
return iss;
}
int main()
{
std::vector<uint32_t> vi(3);
std::istringstream iss;
iss >> vi;
for (std::vector<uint32_t>::iterator it = vi.begin(); it != vi.end(); ++it)
std::cout << *it << std::endl;
} …Run Code Online (Sandbox Code Playgroud) c++ containers templates operator-overloading extraction-operator
c++ ×6
templates ×2
aggregate ×1
assign ×1
boolean ×1
boost ×1
buffer ×1
const ×1
constructor ×1
containers ×1
inheritance ×1
initializer ×1
paste ×1
replace ×1
template-argument-deduction ×1
typeof ×1
types ×1
vector ×1
vim ×1