我有一个关于c ++ 11指针的问题.具体来说,如何将基类的唯一指针转换为派生类?
class Base
{
public:
int foo;
}
class Derived : public Base
{
public:
int bar;
}
...
std::unique_ptr<Base> basePointer(new Derived);
// now, how do I access the bar member?
Run Code Online (Sandbox Code Playgroud)
它应该是可能的,但我无法弄清楚如何.每次我尝试使用
basePointer.get()
Run Code Online (Sandbox Code Playgroud)
我最终得到了可执行文件崩溃.
在此先感谢,任何建议将不胜感激.
是否int a=1, b=a++;调用未定义的行为?在初始化器中初始化a及其访问和修改之间没有序列点介入b,但据我所知,初始化不是对象的"修改"; 指定初始值设定项以提供对象的"初始值".根据6.7.8初始化,第8段:
初始值设定项指定存储在对象中的初始值.
在对对象进行任何访问之前,将"initial"作为顺序进行似乎是合理的.此问题是否已经考虑过,是否有可接受的解释?
我必须使用水平顺序遍历打印二叉树的节点,但是以螺旋形式打印,即不同级别的节点应以螺旋形式打印.
例如:如果树看起来像:

输出应为10 5 20 25 15 6 4.
我使用的算法很简单,只是级别顺序遍历的一个小变化.我只取一个变量p.如果变量等于1,则从左到右打印给定级别的顺序,如果从右到左打印-1.
void getlevel(struct node *root,int n,int p)
{
if(root==NULL)
return;
if(n==0)
{
printf("%d ",root->info);
return;
}
if(n>0)
{
if(p==1)
{
getlevel(root->left,n-1,p);
getlevel(root->right,n-1,p);
}
if(p==-1)
{
getlevel(root->right,n-1,p);
getlevel(root->left,n-1,p);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了答案,但最坏的情况复杂性可能是在偏斜树的情况下O(n ^ 2).
这个任务可以有更好的算法吗?
我的整个节目都在这里
我的问题位于我的代码评论中:
int* a = new int[0];// I've expected the nullptr according to my logic...
bool is_nullptr = !a; // I got 'false'
delete[] a; // Will I get the memory leaks, if I comment this row?
Run Code Online (Sandbox Code Playgroud)
谢谢.
当数据不适合内存时,网上有很多关于在Unix上对大文件进行排序的讨论.通常使用mergesort和variants.
Hoewever,如果假设有足够的内存来容纳整个数据,那么最有效/最快的排序方式是什么?csv文件大约为50 GB(> 10亿行),并且有足够的内存(数据大小的5倍)来保存整个数据.
我可以使用Unix排序,但仍然需要> 1小时.我可以使用任何必要的语言,但我主要寻找的是速度.我知道我们可以将数据加载到一个柱状类型的db表和排序中,但这是一次性的努力,所以寻找更灵活的东西......
提前致谢.
我最近在一次采访中被问到,只使用位移操作符,编写一些代码,告诉你一个数字是否可被8整除,显然代码很短 - 有没有人有线索?
据我所知,\在C中只是附加下一行,好像没有换行符.
请考虑以下代码:
main(){\
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我看到预处理的代码(gcc -E)时,它会显示出来
main(){return
0;
}
Run Code Online (Sandbox Code Playgroud)
并不是
main(){return 0;
}
Run Code Online (Sandbox Code Playgroud)
这种行为的原因是什么?另外,我如何获得我期望的代码?
我试图用C++编写代码,就像tail -f在linux中一样.我发现了这个问题:
如何在C++中阅读不断增长的文本文件?并实现了相同的.我创建了一个temp.txt并开始做echo "temp" >> temp.txt.但是我的程序没有打印对文件所做的更新.我做错了什么?这是我正在使用的代码
#include <iostream>
#include <string>
#include <fstream>
#include <unistd.h>
int main()
{
std::ifstream ifs("temp.txt");
if (ifs.is_open())
{
std::string line;
while (true)
{
while (std::getline(ifs, line)) std::cout << line << "\n";
if (!ifs.eof()) break; // Ensure end of read was EOF.
ifs.clear();
sleep(3);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
我在linux机器上尝试了相同的代码并且它工作正常,但它不适用于Mac.我gcc用来编译代码.
gcc -v 给
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
Target: …Run Code Online (Sandbox Code Playgroud) 考虑以下代码C++:
#include<iostream>
using namespace std;
class Test {
int &t;
public:
Test (int &x) { t = x; }
int getT() { return t; }
};
int main()
{
int x = 20;
Test t1(x);
cout << t1.getT() << " ";
x = 30;
cout << t1.getT() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用gcc编译器时显示以下错误
est.cpp: In constructor ‘Test::Test(int&)’:
est.cpp:8:5: error: uninitialized reference member ‘Test::t’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
为什么编译器不直接调用构造函数?
c++ ×5
c ×2
new-operator ×2
algorithm ×1
binary-tree ×1
bit-shift ×1
c++11 ×1
calloc ×1
clang ×1
clang++ ×1
gcc ×1
llvm-clang ×1
macos ×1
memory-leaks ×1
oop ×1
pointers ×1
sorting ×1
unix ×1