我想和std::array混在一起,看看它和std::vector有多大的不同。到目前为止,我只发现了一个主要的区别。
Sentence sentence = { "Hello", "from", "GCC", __VERSION__, "!" };
std::array<std::string, 10> a;
std::copy(sentence.begin(), sentence.end(), a.begin());
int i = 0;
for (const auto& e : a)
{
i
为什么sizeof操作符产生12个字节,而它应该只有4个字节?当我引用变量array时,它只引用数组的第一个索引的内存地址。实际上,我打印了第一个索引&array[0]的内存地址,并将其与array进行了比较,它们产生了相同的内存地址结果,这证实了它们都引用了数组的第一个索引,但是“数组”生成12字节,而array[0]生成4字节。
int main() {
int array[] = {1,2,3};
int a = 1;
int b = sizeof(array); //this is referring to the first index of the array
int c
为什么这是一个错误:
typedef int H[4];
H * h = new H; // error: cannot convert 'int*' to 'int (*)[4]' in initialization
此外,为什么这不是一个错误:
H * h = new H[1];
为什么编译器认为new H返回int *,而new H[1]按预期返回H *?
换句话说:为什么T * t = new T; T 对于一般类型T是正确的,但是当T是数组类型时却是不正确的?
通过new分配简单数组类型的规范方法是什么?
请注意,这是一个简化的示例
void Merge(int *array, int lo, int mid, int hi) {
int tArray[20];
int loBeg = lo;
int count = lo;
int hiBeg = mid + 1;
while (loBeg <= mid && hiBeg <= hi) {
if (array[loBeg] < array[hiBeg]) {
tArray[count] = array[loBeg];
loBeg++;
为什么下面的代码给出24作为一个答案?也就是说,以下类X的对象的总大小是24字节?我用的是64位机。
#include <bits/stdc++.h>
using namespace std;
class X
{
vector <bool> f;
int b;
public:
X(){
f.push_back(true);
}
};
int main(){
X ob;
cout<<sizeof(ob);
return 0;
}
我读过关于分段错误的文章,但我不明白为什么会有这样的错误是由以下代码引起的。
#include<iostream>
#include <stdlib.h>
using namespace std;
int main(){
int input;
cout << "Enter length of desired array." << "\n";
cin >> input;
int A [input];
//Populate and print the Array.
for(i
有人能解释为什么在C++中声明动态2D数组时使用指向指针(**)的指针吗?我不明白为什么我们要用循环。
using namespace std;
int main(){
int **matrixPtr;//2d array declaretion
cout << "enter no of rows: ";
int row;//defining row
cin >> row;//initializing value of row from user
cout << "enter no of c
我不太确定如何提出这个问题,所以我将从一些示例代码开始:
//header file
class A
{
public:
A();
private:
int x;
std::string arr[x];
}
//cpp file
class A
{
public:
A()
{
/*code to get the value of x from a cmd call*/
}
}
此代码有效吗?更具体地说,我可以让我的头文件中的字符串数组的大小为x吗,即使在创建A对象之前x没有明确的赋值?
如果这不起作用,我唯一的其他选择是使用动态分配
我对布局std140和std430之间的差异有困难。
这是我在.Cpp中的.Cpp代码
struct Particle {
glm::vec3 position = glm::vec3(0);
float density = 1;
};
for (int z = 0; z < d; ++z) {
for (int y = 0; y < d; ++y) {
for (int x = 0; x < d; ++x) {
int index = z * d * d + y * d + x;
i
我正在编写一个函数,该函数应该在"-“上拆分一个字符串,然后返回包含字符串中的各个部分的数组,所以如果字符串是: 2017-10-23,我希望数组包含三个元素,如: arr = 2017,arr1 = 10,arr2 = 23。
这是一项功能:
/*
* return a array of the parts of date_string
*/
int * to_date_array(char *date_string)
{
int i = 0;
int f = 0;
char *tokens = strtok(date_str
我将变量arrSize赋值为零( int arrSize = 0; ),在下一行中,我从用户那里获取变量arrSize的输入。假设用户将5 value赋值给变量。
当我执行我的代码时,Cin只接受4 values而不是5。
但是,当我只声明变量( int arrSize; )而没有给它赋值时,它工作得很好。
我是个新手,你能解释一下为什么会这样吗?
谢谢!
下面是我的代码:
#include <iostream>
using namespace std;
int main(){
int arrSize = 0;
int arr[arrSize];
我知道C99 (以及C++)之前的C标准规定,堆栈上的数组的大小必须在编译时已知。但是为什么呢?堆栈上的数组是在运行时分配的。那么为什么大小在编译时很重要呢?希望有人能向我解释一下编译器在编译时会如何处理大小。谢谢。
这类数组的示例如下:
void func()
{
/*Here "array" is a local variable on stack, its space is allocated
*at run-time. Why does the compiler need know its size at compile-time?
*/
嗯,我在互联网上搜索了动态扩展数组,发现这在C++中是不可能的。我必须使用malloc、C版本的realloc或C++中的std::vector。但是下面的代码似乎工作得很好。
#include<iostream>
using namespace std;
int main() {
int len = 10;
int *p = new int[len];
for (int i = 0; i < len; i++){
p[i] = i + 1;
}
*(p + len) = 1000; // extend here.