我有以下代码:
#include "stdafx.h"
#include <unordered_map>
#include <cassert>
int main()
{
struct Foo { int a; };
std::unordered_map<int, Foo> foos{ { 0, { 3 } }, { 1, { 4 } } };
for (auto &[i, foo] : foos)
{
foo.a = 6; //doesn't change foos[i].a
assert(&foo.a == &foos[i].a); //does not pass
}
auto &[i, foo] = *foos.begin();
foo.a = 7; //changes foo[0].a
assert(&foo.a == &foos[0].a); //passes
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
为什么第一个断言语句在第二个传递时没有通过?为什么我不能改变的价值foo在foos地图中基于范围循环?
编译: MSVS ++ 17 Visual studio 15.3.2 …
我有以下课程:
//in some .h file
#define BARS_IN_FOO 5 //The only place where this number should be specified.
//All code should work when I change this
//in some .cpp file
struct Foo;
struct Bar { Foo & foo; Bar(Foo & foo) : foo{ foo } {} } //Cannot be default initialized
struct Foo {
std::array<Bar, BARS_IN_FOO> myBars;
Foo() :
myBars{ } //Error. Cannot default initialize Bars.
//I want to initialize all Bars with Bar{*this} but at this point I don't
//know …Run Code Online (Sandbox Code Playgroud) 作为 C++ 程序员,以下代码对我来说似乎很自然,但它无法运行:
from typing import TypeVar, Generic, List, NewType
TPopMember = TypeVar('TPopMember')
Population = NewType('Population', List[TPopMember])
class EvolutionaryAlgorithm(Generic[TPopMember]):
def __init__(self, populationSize: int) -> None:
# The following raises TypeError: 'TypeVar' object is not callable
self.__population = Population([TPopMember() for _ in range(populationSize)])
Run Code Online (Sandbox Code Playgroud)
显然,Python 无法实例化实际上是 TypeVars 的类(TPopMember)。我只是想创建一个列表(人口),其中有几个默认初始化(在 Python 中你怎么说?)TPopMembers。我应该怎么做?
我正在使用 Python 3.7.2。
这可能是一个愚蠢的问题,我忽略了一个明显的答案,但如果我有以下内容:
interface A { }
struct B {
A some_field;
}
Run Code Online (Sandbox Code Playgroud)
然后,B的默认ctor会让我看到它的大小是多少?
换句话说(从另一个角度来看),接口类型的默认值是什么,它会被视为引用或值类型,因为类(引用)和结构(值)都可以从它继承?
我以为我知道如何处理c ++中的内存管理,但这让我很困惑:
请考虑以下代码:
struct A {
int i;
};
int main(int argc, char* argv[]) {
A a{ 5 }; //Constructs an A object on the stack
A* b = new A{ 7 }; //Constructs an A object on the heap and stores a pointer to it in b
A* c = new A[] { //Construct an array of A objects on the heap and stores a pointer to it in c
{ 3 },
{ 4 },
{ 5 }, …Run Code Online (Sandbox Code Playgroud) 我正在 VSCode 中设置一个在 Node 16 中运行的新 Typescript 项目。我正在尝试让调试器工作。
按 F5 时出现此错误:
Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
Run Code Online (Sandbox Code Playgroud)
当我在 package.json 中将 type 设置为 Module 时,我得到:
Uncaught TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for c:\Users\peterB\repositories\BlockTanks\blocktanks-bots\src\main.ts
Run Code Online (Sandbox Code Playgroud)
我的设置:
node ./dist_dev/main.js成功运行我的代码。我的 tsconfig.json:
{
"references": [
{ "path": "src" }
],
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */ …Run Code Online (Sandbox Code Playgroud) 假设我有以下课程:
#include <vector>
class Foo
{
public:
Foo(const std::vector<int> & a, const std::vector<int> & b)
: a{ a }, b{ b } {}
private:
std::vector<int> a, b;
};
Run Code Online (Sandbox Code Playgroud)
但是现在我想考虑构造函数的调用者可能会将临时变量传递给它的情况,并且我想将这些临时变量正确地移动到a和b。
现在我真的需要再添加 3 个构造函数,其中 1 个a作为右值引用,其中 1 个b作为右值引用,1 个只有右值引用参数吗?
当然,这个问题可以推广到任何数量的值得移动的参数,并且所需构造函数的数量将是参数^2 2^参数。
这个问题也适用于所有函数。
这样做的惯用方法是什么?还是我在这里完全遗漏了一些重要的东西?
c++ ×3
arrays ×2
c# ×1
c++11 ×1
c++17 ×1
constructor ×1
for-loop ×1
heap ×1
interface ×1
node.js ×1
python ×1
python-3.x ×1
struct ×1
structure ×1
type-hinting ×1
typescript ×1
visual-c++ ×1