说每次使用强制转换时,生成的对象是一个const对象是否正确?
...因此,如果该函数将其作为const对象接受,则只能用作函数的参数?
例如
class C1 {
public: C1(int i=7, double d = 2.5){};
};
void f(C1& c) {};
int main(){
f(8);
return 1;
}
//won't compile
Run Code Online (Sandbox Code Playgroud)
(当然,如果f(....)按值接收参数,那么它将获得一个非常可用的非const版本)
我正在使用free来释放在递归函数中为一堆临时数组分配的内存.我会发布代码,但它很长.当我注释掉这些free()调用时,程序运行不到一秒钟.但是,当我使用它们时,程序运行大约需要20秒.为什么会发生这种情况,如何解决?这就像100左右的MB所以我宁愿不要只留下内存泄漏.
此外,当我运行包含所有启用了性能分析的free()调用的程序时,它会在不到一秒的时间内运行.我不知道这会产生什么影响,但确实如此.
在仅使用一些free()调用之后,似乎有一些特别会导致程序变慢.其余的似乎没有效果.
好的......这是所要求的代码:
void KDTree::BuildBranch(int height, Mailbox** objs, int nObjects)
{
int dnObjects = nObjects * 2;
int dnmoObjects = dnObjects - 1;
//Check for termination
if(height == -1 || nObjects < minObjectsPerNode)
{
//Create leaf
tree[nodeIndex] = KDTreeNode();
if(nObjects == 1)
tree[nodeIndex].InitializeLeaf(objs[0], 1);
else
tree[nodeIndex].InitializeLeaf(objs, nObjects);
//Added a node, increment index
nodeIndex++;
return;
}
//Save this node's index and increment the current index to save space for this node
int thisNodeIndex = nodeIndex;
nodeIndex++;
//Allocate memory for split …Run Code Online (Sandbox Code Playgroud) 考虑以下:
for (it = list.begin(); it != list.end(); ++it) {
if (!(*it)->alive) {
it = list.erase(it);
}
}
Run Code Online (Sandbox Code Playgroud)
这个工作正常list.size() > 1.一旦列表只包含一个元素,则调用erase segfaults.我假设因为没有下一个元素.至少,这是我观察到的行为.有没有正确的方法来处理这个?也许是完全不同的方法?
如您所见,我不想立即清除列表.我想在它们死亡时删除元素,这样可以正常工作,直到它删除最后一个元素.
我创建了一个C++程序,以测试通过函数引用传递参数的功能.
#include <iostream>
using namespace std;
int f(int &b) {
b = b + 1;
cout << b << endl;
return b;
}
int main() {
int t = 10;
cout << f(t) << " " << t << endl;
//cout << f(&t) << " " << t << endl;
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
能否请您解释为什么这个程序不会影响t执行f函数后的值?b传递的参数是引用,所以我认为它的值会在程序执行后发生变化,因为我正在使用main函数中的实际变量,而不是它的副本.在这种情况下,我希望它是11,但它不受程序执行的影响.
为什么会这样?
好吧,我通常默认假设错误在我的代码中,但我所看到的对我来说几乎没有意义.
我有一个std::vector<DocumentWidget *>,我希望通过一些相对简单的算法进行排序.这就是我所看到的.
代码如下所示:
std::vector<DocumentWidget *> documents = DocumentWidget::allDocuments();
// NOTE(eteran): on my system, I have observed a **consistent** crash
// when documents.size() == 17 while using std::sort.
std::sort(documents.begin(), documents.end(), [](const DocumentWidget *a, const DocumentWidget *b) {
int rc = (a->filenameSet_ == b->filenameSet_) ? 0 : a->filenameSet_ && !b->filenameSet_ ? 1 : -1;
if (rc != 0) {
return rc < 0;
}
if(a->filename_ < b->filename_) {
return true;
}
return a->path_ < b->path_;
});
Run Code Online (Sandbox Code Playgroud)
看起来很简单,但是当我在列表中有第17个项目时它会崩溃!排序谓词显然没有vector以任何方式修改,所以我看不出这是一个问题.地址清理器和valgrind在此之前没有显示任何错误. …
我对mod_rewrite比较新,但有一个网站,我希望有"漂亮的网址".与SO相似:).
我试图把这样的东西:" http://www.whatever.com/search/test "重写为" http://www.whatever.com/search.php?q=test "并且有一些限制成功.我相信内容谈判正在阻碍我......
对于初学者来说,这是我的测试.htaccess文件:
RewriteEngine on
RewriteBase /~user/mysite/
RewriteRule ^search$ search/ [R]
RewriteRule ^search/([^/]*)/?$ search.php?q=$1 [L]
Run Code Online (Sandbox Code Playgroud)
不幸的是,它重定向到search.php,但没有在q变量中传递我的参数.但这确实有效:
RewriteEngine on
RewriteBase /~user/mysite/
RewriteRule ^search$ search/ [R]
RewriteRule ^search/([^/]*)/?$ s.php?q=$1 [L] # here i've renamed the search.php to s.php to dodge the content negotiation that is happening..
Run Code Online (Sandbox Code Playgroud)
事实上,如果我一起删除规则,我得到的结果与文件的第一个版本相同.所以我的结论是,即使没有任何mod_rewrite规则,apache很乐意将"foo"重定向到"foo.php",它必须是正在处理它的内容协商.(如果我将我的foo.php重命名为foo.html,它仍然会找到该文件,如果我只是去"foo"),这进一步验证了这一点.
所以,问题是.如何在内容协商方面正确使用mod_rewrite?我可以为特定文件禁用它吗?有没有办法确保我的mod_rewrite规则在内容协商发生之前发生?
如果它是相关的,这里是我的apache conf的mod_userdir部分的conf文件(这个测试站点在我的用户的homedir/public_html中):
# Settings for user home directories
<IfDefine USERDIR>
<IfModule userdir_module>
# UserDir: The name of the directory that is appended onto a user's home …Run Code Online (Sandbox Code Playgroud) 在c#我可以声明object o;然后我可以分配o=(float)5.0;或o="a string."是否有Objective-C的等价物?我尝试使用,id但它不采用像float或integer这样的原始类型.谢谢你的帮助.
当我尝试用C++编写代码时
cout << char(219);
Run Code Online (Sandbox Code Playgroud)
我的mac上的输出是问号?然而,在PC上,它给了我一个黑色方块.有没有人知道为什么在Mac上只有128个字符,什么时候它应该是256?谢谢你的帮助.
假设我有一个数字范围,比如{2,3,4,5},按顺序存储在a中std::vector v,并且我想列出所有可能的子集,以5结尾使用STL ...即:
2 3 4 5
2 3 5
2 4 5
3 4 5
2 5
3 5
4 5
5
Run Code Online (Sandbox Code Playgroud)
(我希望我不要忘记任何:))
我试过用while(next_permutation(v.begin(),v.end()))但没想出想要的结果:)
有没有人有想法?
PS:那些已经完成谷歌代码堵塞2010年档案的人可能会认识到这一点:)
#include <stdio.h>
int main()
{
int m,n; scanf("%d %d",&m,&n);
char ar[m][n];
char buf[n];
int a,b;
for(a=0;a<m;a++)
{
gets(buf);
for(b=0;b<n;b++) ar[a][b] = buf[b];
}
for(a=0;a<m;a++,printf("\n")) for(b=0;b<n;b++) printf("%c",ar[a][b]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码从m行作为输入stdin,每行包含n个字符,并打印所有行stdout.就那么简单.但似乎存在内存泄漏,因为gets(buf)遇到第一次,它的执行被跳过.
我也在C++中尝试过,认为内存泄漏会消失.这是代码:
#include <cstdio>
using namespace std;
int main()
{
int m,n; scanf("%d %d",&m,&n);
char **ar = new char*[m];
char *buf = new char[n];
int a,b;
for(a=0;a<m;a++)
{
gets(buf);
ar[a] = new char[n];
for(b=0;b<n;b++) ar[a][b] = buf[b];
}
for(a=0;a<m;a++,printf("\n")) for(b=0;b<n;b++) printf("%c",ar[a][b]); …Run Code Online (Sandbox Code Playgroud) c++ ×7
c ×2
stl ×2
ambiguity ×1
apache ×1
ascii ×1
c# ×1
c++11 ×1
casting ×1
char ×1
combinations ×1
macos ×1
memory-leaks ×1
mod-rewrite ×1
objective-c ×1
sorting ×1
stdlist ×1
types ×1
vector ×1