我遇到了以下代码(粗略地):
struct StringBuffer {
StringBuffer(const char* string) {strcpy(m_buffer, string);}
const char* c_str() const {return m_buffer;}
char m_buffer[128];
};
std::string foobar() {
const char* buffer = StringBuffer("Hello World").c_str();
return std::string(buffer);
}
Run Code Online (Sandbox Code Playgroud)
假设在行之后我是否正确:
const char* buffer = StringBuffer("Hello World").c_str();
Run Code Online (Sandbox Code Playgroud)
buffer指向解构StringBuffer对象中的指针?
我有一个显示在属性网格中的类.其中一个属性是List<SomeType>.
设置代码的最简单/最正确的方法是什么,以便我可以通过属性网格添加和删除此集合中的项目,最好使用标准CollectionEditor.
错误的方法之一是这样的:
用户annakata建议我公开一个IEnumerable接口而不是一个集合.有人可以提供更多细节吗?
我有额外的复杂性,返回的集合get实际上并没有指向我的类中的成员,而是从其他成员动态构建,如下所示:
public List<SomeType> Stuff
{
get
{
List<SomeType> stuff = new List<SomeType>();
//...populate stuff with data from an internal xml-tree
return stuff;
}
set
{
//...update some data in the internal xml-tree using value
}
}
Run Code Online (Sandbox Code Playgroud) A std::map必须满足第23.1.2/2段规定的关联容器的要求:
每个关联容器都在Key上进行参数化和一个排序关系比较,它对Key的元素产生严格的弱排序(25.3).此外,map和multimap将任意类型T与Key相关联.Compare类型的对象称为容器的比较对象.该比较对象可以是指向函数的指针或具有适当函数调用操作符的类型的对象.
但是在第23.3.1/2段中,std::map模板被指定为:
template <class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key, T> > >
class map;
Run Code Online (Sandbox Code Playgroud)
这似乎明确禁止使用函数指针Compare.这是矛盾还是我不能正确理解标准?
编辑:是的,我真正遇到的问题是为什么代码像GMan的例子:
struct foo
{
int x;
};
bool compare_foo(const foo& x, const foo& y)
{
return x.x < y.x;
}
std::map<foo, bool, compare_foo> fooMap;
Run Code Online (Sandbox Code Playgroud)
不会编译(是的,我愚蠢地混淆了Compare参数的类型和值).
我想检查一个\n终止是否string只包含空格.我使用以下正则表达式尝试失败:
new Regex(@"\s*\n")
Run Code Online (Sandbox Code Playgroud) 这里有两段代码,我起初认为应该是等价的:
{
std::ifstream stream("test.bin", std::ios_base::in | std::ios_base::binary);
unsigned char count = 128;
unsigned char read = 0;
unsigned char scanline[128];
long long start = stream.tellg();
while (count--) {
stream >> scanline[read++]; // <---- This is the only line which differs
}
long long end = stream.tellg();
std::cout << end - start << "\n";
}
{
std::ifstream stream("test.bin", std::ios_base::in | std::ios_base::binary);
unsigned char count = 128;
unsigned char read = 0;
unsigned char scanline[128];
long long start = stream.tellg();
while (count--) …Run Code Online (Sandbox Code Playgroud) 我可以通过使用不同的返回值来重载或覆盖方法吗?在这种情况下是虚拟的事情?
例如 :
class A{
virtual int Foo(); // is this scenario possible with/without the keyword virtual
}
class B : public A {
virtual double Foo();
}
A* Base = new B();
int i = Base->Foo(); // will this just convert double to int ?
Run Code Online (Sandbox Code Playgroud)
并且有关重载:
class A{
virtual int Foo();
virtual float Foo(); // is this possible ?
int Goo();
float Goo(); // or maybe this ?
}
Class B{
double Foo();
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我遇到了问题,似乎无法找到解决方案..
int linearSearch(nodeptr list,char search){
int pos =0;
if(list==NULL)
return -1;
while(list->info!=search && list!=NULL){
pos++;
list=list->next;
}
if(list==NULL)
return -1;
else
return pos;
}
Run Code Online (Sandbox Code Playgroud)
我总是得到运行时错误.. :(
//sample.h
int calci(int &value)
{
if(value < 20)
throw value;
else
return value;
}
class XYZ
{
int m_x;
public: XYZ(int &x)try:m_x(x-calci(x))
{
}catch (int &a)
{}
};
class ABC
{
int m_a;
public: ABC():m_a(0)
{
}
void foo()
{
XYZ xyz(10);
}
};
int main()
{
ABC abc;
abc.foo();
}
Run Code Online (Sandbox Code Playgroud)
//如果我用以下代码替换foo(),那么它运行良好
void foo()
{
try{
XYZ xyz(10);
}catch(...){}
}
Run Code Online (Sandbox Code Playgroud)