如何将int*转换或转换为int [x]?
作为一个例子,我试图将指针int* c = new int[x]
转换为数组int b[2]
int a = 1;
int b[2] = { 2, 3 };
int* c = new int[b[1]];
c[0] = b[0];
c[1] = b[1];
c[2] = a;
Run Code Online (Sandbox Code Playgroud)
首先,我知道我可以遍历指针和数组.因此,将指针转换为具有[]
运算符的数组,以索引指针和数组,在迭代时将指针元素分配给数组元素(例如arr[i] = p[i]
).我想知道在更少的代码行中是否可以实现相同的结果.
我试图自己搞清楚,所以我做了一个快速的程序,以确保我确切地知道我想要放置什么和在哪里.输出如下:
Address of {type: int} &a = 0031FEF4; a = 1
Address of {type: int[2]} &b = 0031FEE4; b = 0031FEE4
Address of {type: int[2]} &b[0] = 0031FEE4; b[0] = 2
Address of {type: int[2]} …
Run Code Online (Sandbox Code Playgroud) 我的编译器找不到我定义的命名空间.我有四个文件和3个命名空间.
charon.cpp
chin.cpp
chout.cpp
main.cpp中
我命名空间是戎,charon_in和charon_out.主要问题出现在一个特定的文件charon.cpp上,所以这里也是那个文件和chin.cpp.
错误:
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/sys/charon.o.d -o build/Debug/GNU-Linux-x86/sys/charon.o sys/charon.cpp
sys/charon.cpp:6:17: error: ‘charon_in’ is not a namespace-name
sys/charon.cpp:6:26: error: expected namespace-name before ‘;’ token
sys/charon.cpp:7:17: error: ‘charon_out’ is not a namespace-name
sys/charon.cpp:7:27: error: expected namespace-name before ‘;’ token
sys/charon.cpp:15:5: error: ‘chout_’ does not name a type
sys/charon.cpp:16:5: error: ‘chin_’ does not name a type
sys/charon.cpp:31:39: error: ‘chin_’ has not been declared
sys/charon.cpp:31:55: error: …
Run Code Online (Sandbox Code Playgroud) 好的,所以我原本试图使用一些据说只用于Windows的标题,我的不好,但我已经离开了,只是使用curses.h复制了我需要的东西.但是我仍然收到完全相同的错误.
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory `/home/josh/Projects/Testing grounds/kbhit'
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/kbhit
gmake[2]: Entering directory `/home/josh/Projects/Testing grounds/kbhit'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/kbhit build/Debug/GNU-Linux-x86/main.o
build/Debug/GNU-Linux-x86/main.o: In function `kbhit()':
/home/josh/Projects/Testing grounds/kbhit/main.cpp:20: undefined reference to `stdscr'
/home/josh/Projects/Testing grounds/kbhit/main.cpp:20: undefined reference to `wgetch'
/home/josh/Projects/Testing grounds/kbhit/main.cpp:23: undefined reference to `ungetch'
collect2: ld returned 1 exit status
gmake[2]: *** [dist/Debug/GNU-Linux-x86/kbhit] Error 1
gmake[2]: Leaving directory …
Run Code Online (Sandbox Code Playgroud) struct iof_header
我的代码中有一个,我确定它将是24字节宽.我执行sizeof(iof_header)并返回32字节宽.
问题1 为什么它是32字节宽而不是24字节?
问题2 包括其成员,结构如何存储在内存中?
问题3
我发现任何时候我创建我的一个结构,字节[4-8和20-24]都是NULL,我在char数组中看到这一点.数组如下所示{4 bytes of BASEID_Code, 4 NULL bytes, 8 bytes of zeroed padding, 4 bytes of ASID_Code, 4 NULL bytes, 8 bytes of size}
在我的unsigned __int32
成员的末尾有空字节,为什么会发生这种情况?
这可能与编译相关吗?使CPU能够更快地处理这些数据类型可能是一种有效的方法吗?
struct iof_header
{
union
{
struct
{
unsigned __int32 BASEID_Code;
unsigned __int64 padding;
union
{
char ASID_Type[4];
unsigned __int32 ASID_Code;
};
unsigned __int64 Size;
}header;
char header_c[24];
};
iof_header()
{
header.ASID_Code = 0;
header.BASEID_Code = 0;
header.Size = 0;
header.padding = 0;
} …
Run Code Online (Sandbox Code Playgroud) 我想弄清楚这个编译错误意味着什么,我希望我能解释清楚.
In file included from sys/charon.cpp:4:0:
Run Code Online (Sandbox Code Playgroud)
接下来将我带到上面的^文件,并用黄色下划线:
#include "../headers/charon.h"
Run Code Online (Sandbox Code Playgroud)
同样,类型标识符"charon_"(在头文件charon.h中定义的类)也以黄色加下划线可能是不同的错误.
sys/../headers/charon.h:17:9: error: redefinition of ‘class charon::charon_’
sys/../headers/chio.h:86:9: error: previous definition of ‘class charon::charon_’
sys/charon.cpp:12:20: error: definition of implicitly-declared ‘charon::charon_::charon_()’
Run Code Online (Sandbox Code Playgroud)
但是我希望它们都与第一个错误有关,我认为它与我想要做的事情有关.
//File 1.h
/**********/
class I
{
private:
B* my_private_b;
public:
I(B& handle);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我需要在"文件1"开始定义"我"之前声明"B"..但同时,我需要为B定义.所以我不知道如何在另一个之前定义两个..我我猜我需要一个更先进的解决方案,但我不知道.
//File 2.h
/***********/
class B
{
private:
I i;
O o;
public:
B();
}
Run Code Online (Sandbox Code Playgroud)
所以,如果我能找到答案,那么也许我可以自己查看下一部分.如果您认为检查我是否朝着正确的方向前进是一件好事,我会在下面粘贴所有相关代码.
不够长,没读过?那么这里是除了注释之外的所有四个文件的代码.按照感知的重要性.
//////////////////////////////////////
/////
/////////////
/////
/////
/////
//File: chio.h
#ifndef CHIO_H
#define CHIO_H
#include <deque>
#include <queue>
#include <iostream> …
Run Code Online (Sandbox Code Playgroud) 我的问题:我想用 std::stack<std::pair<int,int>, std::unordered_set<std::pair<int,int>>
或std::stack<coords, std::unordered_set<coords>>
简称
我想知道是否可以扩展std::unordered_set<>
,以便它可以在没有问题的情况下运行std::stack<>
.除此之外,我想知道它是否值得付出努力,而不泄露实际的最终用途.我只是意味着使用unordered_set的好处是使用必要的方法设计我自己的模板更有效,而不是扩展现有的unordered_set以满足要求?
编辑:通常我会删除一个否定的问题,但我觉得这可能最终有助于其他一些误解了std :: unordered_set的可怜的灵魂.然后我还没死,所以我可能会删除它.
编辑2:来自AndreyT的答案有助于弄清楚为什么std::stack<std::pair<int,int>, std::unordered_set<std::pair<int,int>>
不能成为一件事.我切换到jxh的答案,因为它基本上实现了一个并且接近我最终使用的.