如何声明未知STL容器的迭代器?例如,我想编写一个接收容器的函数,并使用迭代器将其打印出来:
template <class Container> void print(Container c) {
// how to declare iterator???????
my_iterator = c.begin();
while(my_iterator!=c.end()) {
cout << *my_iterator << endl;
my_iterator++;
}
}
Run Code Online (Sandbox Code Playgroud) 我如何获得外部实例?
class OuterClass {
class InnerClass {
class MoreInnerClass {
public MoreInnerClass() {
// i want to get outer class from here
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想从MoreInnerClass方法中获取OuterClass实例.
假设我有一个包含999个单元的数组,其中包含1-1000之外的所有数字,除了一个数字.找到这个数字最有效的方法是什么?我找不到比O(n平方)更好的方法.面试官告诉我有更好的方法.我该怎么做?
数组未分类.
我可以从JFrame中删除边框以及最小化和关闭按钮吗?我不能使用JWindow,因为它无法处理我理解的keyListener.(至少不容易).我怎样才能做到这一点?
假设我使用此代码:
int *pointer;
if(1) {
int num = 5; // local variable, can't be used outside the if block.
pointer = &num
}
Run Code Online (Sandbox Code Playgroud)
这是一种跟踪num变量的安全方法吗?我知道这段代码会起作用.但我认为编译器将使用旧num内存来分配新变量,从而导致pointer引用不可预测的值.真的吗?
我知道如何通过将其引用变量设置为null来为垃圾收集器留下信号以删除对象:
Player player_reference = new Player();
player_reference = null;
// Now the Garbage collector knows to release all the memory related to this object.
Run Code Online (Sandbox Code Playgroud)
但是如何通过对象的类将引用变量设置为null?
class Player {
public void doSomthing() {
if(condition) {
// some code which set the reference variable to null.
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在实施一个链表.我写了一个拷贝构造函数:
// --- copy constructor ---
IntList(const IntList& last_list) {
first = new IntNode(last_list.getFirst()->getData());
cout << "copy first " << first->getData() << endl;
IntNode* last_node = first;
IntNode* node = last_list.getFirst()->getNext();
while(node!=NULL) {
IntNode* new_node = new IntNode(node->getData());
last_node->setNext(new_node);
cout << "copy " << new_node->getData()<< endl;
last_node = new_node;
node = node->getNext();
}
}
Run Code Online (Sandbox Code Playgroud)
据我了解,我的副本赋值运算符(operator=)应该有2个目标:
我可以通过调用我已编写的析构函数来实现这两个目标,然后调用复制构造函数.我该怎么做?