我有一个关于封装特定类型的快速问题typedef.假设我有一个类,Foo其构造函数具有某个值,但我想使用typedef以下方法隐藏特定类型:
class Foo {
public:
typedef boost::shared_ptr< std::vector<int> > value_type;
Foo(value_type val) : val_(val) {}
private:
value_type val_;
};
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,主要功能仍然必须知道类型(所以它明确使用std::vector<int>):
int main() {
Foo::value_type val(new std::vector<int>());
val->push_back(123);
Foo foo(val);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题,同时仍然避免Foo构造函数中的向量的深层副本?
我想存储指向a中的Base类的指针vector,但是然后将它们用作函数参数,它们充当特定的类,请参见此处:
#include <iostream>
#include <vector>
class Base {};
template<class T>
class Derived : public Base {};
void Foo(Derived<int>* d) {
std::cerr << "Processing int" << std::endl;
}
void Foo(Derived<double>* d) {
std::cerr << "Processing double" << std::endl;
}
int main() {
std::vector<Base*> vec;
vec.push_back(new Derived<int>());
vec.push_back(new Derived<double>());
Foo(vec[0]);
Foo(vec[1]);
delete vec[0];
delete vec[1];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这不编译:
error: call of overloaded 'Foo(Base*&)' is ambiguous
Run Code Online (Sandbox Code Playgroud)
它可以使它工作吗?我需要根据int,double等类型不同地处理向量的元素.
我正在尝试读取一个文本文件(使用perl),其中每行有几条记录,如下所示:
r1c1 & r1c2 & r1c3 \\
r2c1 & r2c2 & r2c3 \\
Run Code Online (Sandbox Code Playgroud)
那么,&是记录分隔符.
Perl的帮助说明了这一点:
$ perl -h
-0[octal] specify record separator (\0, if no argument)
Run Code Online (Sandbox Code Playgroud)
为什么你会使用八进制数超出我.但是046分隔符的八进制ASCII &,所以我尝试了这个:
perl -046 -ane 'print join ",", @F; print "\n"' file.txt
Run Code Online (Sandbox Code Playgroud)
期望的输出将在哪里
r1c1,r1c2,r1c3 \\
r2c1,r2c2,r2c3 \\
Run Code Online (Sandbox Code Playgroud)
但它不起作用.你怎么做的?
我有一个包含向量的类:
class Foo {
typdef std::vector<int> Vec;
Vec m_kids;
void addKids(Vec::const_iterator begin,
Vec::const_iterator end) {
m_kids.insert(m_kids.end(), begin, end);
}
};
Run Code Online (Sandbox Code Playgroud)
有没有办法允许以下简洁的函数调用?(也许通过改变addKids上面的功能?)
int main() {
Foo foo;
foo.addKids(23,51,681); // these...
foo.addKids(3,6,1,4,88,2,4,-2,101); // ...would be nice?!
}
Run Code Online (Sandbox Code Playgroud)
我怀疑你可以使用C++ 0x向量初始化列表吗?但不幸的是,我无法使用C++ 0x.不过,如果有帮助,我可以使用Boost.
我有两节课,Base和Derived.我提供了一个函数,将函数应用于所有元素Base,以及另一个函数将函数应用于以下所有元素Derived:
#include <iostream>
#include <vector>
class Base {
typedef std::vector<int> Vec;
typedef Vec::const_iterator CIter;
CIter begin() const;
CIter end() const;
};
class Derived : public Base {
void add(int);
};
template<class F>
void processBase(const Base& b, F& f) {
for (Base::CIter it = b.begin(); it != b.end(); ++it) {
f(b, *it);
}
}
template<class F>
void processDerived(Derived* d, F& f) {
for (Base::CIter it = d->begin(); it != d->end(); ++it) {
f(d, …Run Code Online (Sandbox Code Playgroud) 我该如何打印std::wstring使用std::wcout?
我尝试以下,这是推荐这里,但它仅适用于印刷本¡Hola!,但不是这个??:
#include <iostream>
#include <clocale>
int main(int argc, char* argv[])
{
char* locale = setlocale(LC_ALL, "");
std::cout << "locale: " << locale << std::endl; // "C" for me
std::locale lollocale(locale);
setlocale(LC_ALL, locale);
std::wcout.imbue(lollocale);
std::wcout << L"¡Hola!" << std::endl; // ok
std::wcout << L"??" << std::endl; // empty :(
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下(这里推荐)也不打印日文字符:
#include <stdio.h>
#include <string>
#include <locale>
#include <iostream>
using namespace std;
int main()
{
std::locale::global(std::locale("")); …Run Code Online (Sandbox Code Playgroud) 如何获得元组元组中所有元素的最大值?
例:
x = ((3, 4, 5), (4.5, 5.5, 6.5), (-1, 0, 1, 10))
# want 10
Run Code Online (Sandbox Code Playgroud) 对于我们每天使用的流行语言和库:一些糟糕的设计,令人尴尬的API或一般糟糕的可用性的例子是什么?设计错误是我们必须付出的,因为它们会引入微妙的错误,我们必须使用尴尬的解决方法或记住不直观的方法来完成工作.
我特别想到的问题是:OO语言中的一个类实际上不应该继承其他类.有一种特殊的操作符使得某种语言难以解析,并且结果证明它是未使用的.一个错误名称或经常用于其他事物的函数(我正在考虑std::getline将字符串标记化).
我不是在寻找那些抨击语言的贡献,并声称Perl或其他语言设计得很糟糕.我更多地寻找具体的例子或关于显然本来应该采取不同的事情的轶事.(也许设计师发现它太晚了,并试图在后续版本中修复它,但必须保持向后兼容性.)
我写了一个小的"懒矢量"类(或延迟矢量),它应该看起来像一个std::vector可用的地方std::vector,但它可以加载它的元素"懒惰",即它将加载元素n(可能还有一些)每当有人访问元素时从磁盘n.(原因是在我的应用程序中,并非所有元素都适合内存.)
这是这个LazyVector类,但是const使用这样一个向量的成员函数存在问题,见下文.
template<class T>
class LazyVector {
std::vector<T> elems_;
void fetchElem(unsigned n){
// load the n-th elem from disk into elems_ etc
}
public:
const T& operator[](unsigned n) const {
fetchElem(n); // ERROR: ... discards qualifiers
return elems_[n];
}
T& operator[](unsigned n) {
fetchElem(n);
return elems_[n];
}
// and provide some other std::vector functions
};
Run Code Online (Sandbox Code Playgroud)
正如我所说,当一个const成员函数要求一个元素时,会出现问题LazyVector.根据性质LazyVector,访问元素不是 const,即它将改变vec下面的向量,这在此上下文中是被禁止的.该foo …
在bash命令行中,我想查找所有名为foo或的文件bar.我试过这个:
find . -name "foo\|bar"
Run Code Online (Sandbox Code Playgroud)
但这不起作用.什么是正确的语法?
如何实现以下OrderElements功能?
char chars[] = {'a', 'b', 'c', 'd', 'e'};
int want_order[] = {2, 4, 3, 0, 1};
int length = 5;
OrderElements(chars, want_order, length);
// chars now contains: c, e, d, a, b
Run Code Online (Sandbox Code Playgroud)
当您可以使用线性额外空间时很容易,但是只能使用恒定的额外空间,即直接对chars元素进行原位排序吗?
PS:这不是考试问题; 我实际上需要这个功能.
澄清:似乎存在对所需元素最终顺序的误解.示例中的结果数组应该具有以下元素,引用原始chars数组:
{chars[2], chars[4], chars[3], chars[0], chars[1]}
Run Code Online (Sandbox Code Playgroud)
是的
{'c', 'e', 'd', 'a', 'b'}.
Run Code Online (Sandbox Code Playgroud) operator<以下课程的正确性是什么?
struct Person {
int height;
int width;
friend bool operator<(const Person&, const Person&);
};
Run Code Online (Sandbox Code Playgroud)
谢谢!
在以下代码中,Foo::add通过函数对象调用函数:
struct Plus {
inline int operator()(int x, int y) const {
return x + y;
}
};
template<class Fct>
struct Foo {
Fct fct;
Foo(Fct f) : fct(f) {}
inline int add(int x, int y) {
return fct(x,y); // same efficiency adding directly?
}
};
Run Code Online (Sandbox Code Playgroud)
这与x+y直接调用的效率相同Foo::add吗?换句话说,在fct(x,y)启用优化的情况下编译时,编译器通常直接替换为实际调用,内联代码吗?