有人可以解释成员函数中的静态变量如何在C++中工作.
鉴于以下课程:
class A {
void foo() {
static int i;
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我声明了多个实例A
,那么调用foo()
一个实例会增加i
所有实例上的静态变量吗?或者只是它被召唤的那个?
我假设每个实例都有自己的副本i
,但是通过一些代码,我似乎另有说明.
我听过很多人说如果容器中预期的元素数量相对较小,最好使用std::vector
而不是std::map
eventHough我只使用容器进行查找而不是迭代.
这背后的真正原因是什么?
显然,map的查找性能不会比矢量的查找性能差(尽管可能是纳秒/微秒),那么它与内存使用情况有关吗?
在虚拟地址空间的分段中,矢量是否比映射更好/更差?
我正在使用随Visual Studio一起提供的STL库(即微软实现)与其他实现有什么不同?
当使用模板模板语法时template <template <typename> class T>
,需要使用关键字class
,因为using typename
会给出以下行的错误:
错误:模板模板参数在参数列表后面需要'class'
在其他地方的关键字typename
,并class
是在声明模板参数的基本情况互换.
您可以争辩说使用模板模板时的要求是提示您应该传递类类型,但情况并非总是如此(特别是在C++ 11引入模板化类型别名之后).
template <template <typename> class T> // 'class' keyword required.
struct Foo {
using type = T<int>;
};
template <typename T>
using type = T (*)();
using func_ptr_t = Foo<type>::type;
Run Code Online (Sandbox Code Playgroud)
typename
在没有模板模板声明允许吗?我有以下代码将成员函数绑定到类的实例:
class Foo {
public:
int i;
void test() {
std::cout << i << std::endl;
}
};
int main() {
Foo f;
f.i = 100;
auto func = std::bind(&Foo::test, std::forward<Foo>(f));
f.i = 1000;
func();
}
Run Code Online (Sandbox Code Playgroud)
但该std::bind
声明并未f
通过引用绑定.调用func
打印"100"而不是"1000"这是我想要的.
但是,如果我更改语句以获取指针它是否有效.
auto func = std::bind(&Foo::test, &f);
Run Code Online (Sandbox Code Playgroud)
但是,这是通过f
由指针我的理解,因为我认为std::bind
需要一个R值的参考Arg&&
(如图所示这里)如何工作的呢?
有人可以解释一下吗?
这是我的两个班级:
public class Firstclass {
public static void main(String args[]) throws InterruptedException {
System.out.println("Main start....");
Secondclass t1 = new Secondclass();
t1.setName("First Thread");
Secondclass t2 = new Secondclass();
t2.setName("Second Thread");
t1.start();
t2.start();
System.out.println("Main close...");
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class Secondclass extends Thread {
@Override
public void run() {
try {
loop();
} catch(Exception e) {
System.out.println("exception is" + e);
}
}
public void loop() throws InterruptedException {
for(int i = 0; i <= 10; i++) {
Thread t = Thread.currentThread();
String …
Run Code Online (Sandbox Code Playgroud) 我写了一个非常小的程序:
#include <array>
#include <tuple>
#include <iostream>
const unsigned int NUM = 500;
void simple()
{
using namespace std;
array<tuple<float, float, float>, NUM> vectors;
}
int main(int argc, char **argv)
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我编译了它g++ -std=c++0x
.
这个版本工作正常,但如果我增加到NUM
50,000,000,g ++使用90%的CPU,我的系统完全冻结.
我知道如果没有足够的堆栈内存,程序可能会在执行期间崩溃.但是为什么编译器会在编译期间冻结?
这是g ++中的错误还是由于某种原因编译器需要在编译期间分配堆栈内存?
我有一个非常基本的问题:返回std::vector<A>
使用std::move
是否是一个好主意?例如:
class A {};
std::vector<A> && func() {
std::vector<A> v;
/* fill v */
return std::move(v);
}
Run Code Online (Sandbox Code Playgroud)
我应该返回std::map
,std::list
..等等......这样?
如何使用单个函数将任意动态链接库(dll)函数加载到std::function
对象中?
例如,我想将两个函数编译成一个dll:
// test.dll
int plusFive(int value) {
return value + 5;
}
void printHello() {
std::cout << "Hello!" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
并在运行时使用以下单个函数加载它们:
// main.cc
#include <functional>
int main() {
std::function<int(int)> func1(loadDllFunc("test.dll", "plusFive"));
std::function<void()> func2(loadDllFunc("test.dll", "printHello"));
}
Run Code Online (Sandbox Code Playgroud) 我正在使用(org.imgscalr.Scalr)库来调整某些图像的大小,但是在调整大小后,背景变为红色.我的代码是:
BufferedImage imagemPng = ImageIO.read(image);
BufferedImage imagemJpg = Scalr.resize(imagemPng, Method.QUALITY, 1920, 937);
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?
谢谢
我在Cell处理器上调整DMA传输的内存时遇到问题.我需要地址的最后4位为0.
我有4个数组,unsigned int
其中每个元素必须在内存中对齐,以便其(十六进制)地址以零结尾.
例如
int main()
{
size_t i;
static unsigned int a[2] __attribute__ ((aligned (16)));
static unsigned int b[2] __attribute__ ((aligned (16)));
static unsigned int c[2] __attribute__ ((aligned (16)));
static unsigned int d[2] __attribute__ ((aligned (16)));
for (i = 0; i < 2; ++i) {
printf("a[%u] = %p\n", &a[i]);
printf("b[%u] = %p\n", &b[i]);
printf("c[%u] = %p\n", &c[i]);
printf("d[%u] = %p\n", &d[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
a[0] = 0x10010b60
b[0] = 0x10010b50
c[0] = 0x10010b40 …
Run Code Online (Sandbox Code Playgroud)