int num = atoi(argv[1]);
unsigned long times[num];
Run Code Online (Sandbox Code Playgroud)
我有这个代码,我认为它不会编译,因为我试图使用命令行参数中的值来分配数组,编译器在编译时不知道.但是我编译了这段代码并且它有效.谁能解释一下这里发生了什么?我误解了静态分配的基本概念吗?
如何将数组的大小设置为与向量的大小相等。这是我的代码:
vector<Point> data_obj;
int my_array[data_obj.size()]
Run Code Online (Sandbox Code Playgroud)
但我收到编译错误说:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
Run Code Online (Sandbox Code Playgroud)
我不明白这个错误。你能提供一些解释吗?
我正在尝试以下代码:
int main() {
int x[10] ;
int a[] = {1,2,3,4,5} ;
int n ;
int b[n] ;
//int c[] ; gives compilation error
cout<<sizeof(x)<<endl ; //prints 40
cout<<sizeof(a)<<endl ; //prints 20
cout<<sizeof(b)<<endl ; //prints 4
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是我定义时到底发生了什么b.我试图阅读类似问题的答案,但我没有得到满意的答复.因为数组是静态创建的,所以在声明它们时必须给出大小.那为什么声明b有效.是否sizeof(b)表明这只是作为一个int pointer
我正在制作一个简单的程序来计算一个数组中可以被3个数组长度整除的数量,并且值由用户确定.
现在我的代码非常好.但是,我只是想检查是否有更快的方法来计算它,这导致编译时间更少?
由于数组的长度为10 ^ 4或更少,编译器所需的时间不到100毫秒.然而,随着它增加到10 ^ 5,它会达到1000毫秒,所以为什么呢?以及如何提高速度?
#include <iostream>
using namespace std;
int main()
{
int N, i, b;
b = 0;
cin >> N;
unsigned int j = 0;
std::vector<unsigned int> a(N);
for (j = 0; j < N; j++) {
cin >> a[j];
if (j == 0) {
}
else {
for (i = j - 1; i >= 0; i = i - 1) {
if ((a[j] + a[i]) % 3 == 0) {
b++;
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试填写文本文档,但是当我这样做时 - 我得到第一行空白.我正在阅读的文本文档如下所示:
3
first fr random 5
second 9
third shazam 2
Run Code Online (Sandbox Code Playgroud)
我已经尝试删除第一个值,所以空白行消失了.所以它相关,但我需要这个价值.
// My code
ifstream in("U2.txt");
ofstream out("U2.txt");
int n;
in>>n;
char vardaiStart[n][21];
in.read(vardaiStart[0], 21);
out << vardaiStart[0]<< endl;
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:
*blank*
first fr random
Run Code Online (Sandbox Code Playgroud)
但我想要的是:
first fr random
Run Code Online (Sandbox Code Playgroud)
所以,在其他项目中,我从现在开始不会使用旧的C,但对于这个项目,我更加[endline]并忽略它,如下所示: in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
一个简单的任务,但我不记得C++的语法.
int array[n];
Run Code Online (Sandbox Code Playgroud)
我需要获取数组的最后5个对象.
array.slice(array.length - 5, 1)??
Run Code Online (Sandbox Code Playgroud) 我想知道在c ++中执行以下代码段的一些替代方法。
int i;
cin >> i;
int arr[i];
Run Code Online (Sandbox Code Playgroud)
我最近开始研究竞争性编程并尝试学习更多。
编辑:对于那些不是cpp的评论。它使用我正在使用的类中正在使用的makefile成功编译,gcc -std=c++11 -o a.exe main.cpp并在输入长度为1时返回到控制台
array length: 1
Run Code Online (Sandbox Code Playgroud) 我已经用dev c ++编写了此代码,并且可以工作,但是当我尝试在Visual Studio中运行它时,会出现错误,例如expression必须具有恒定值。
#include <iostream>
using namespace std;
int main() {
int r, c, j;
int matrix[r][c];
cout << "Enter the size of matrix: ";
cin >> j;
for (r = 0; r < j; r++) {
for (c = 0; c < j; c++) {
cout << "matrix" << "[" << r << "]" << "[" << c << "] = ";
cin >> matrix[r][c];
}
}
cout << "\n";
for (int i = 0; i …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <iterator>
#define ADDR "c:\\Users\\Library2\\Desktop\\Books record\\"
using namespace std;
int main()
{
ifstream fin(ADDR "reportcard.csv", ios::binary);
string line;
int rowCount=0;
int rowIdx=0;
while(getline(fin,line)){
rowCount++;
}
vector<string> data[**rowCount**];//this rowCount gave me "expression must have a constant value"
fin.clear();
fin.seekg(fin.beg);
while(getline(fin,line))
{
stringstream ss(line);
string value;
while(getline(ss,value,',')){
data[rowIdx].push_back(value);
}
rowIdx++;
}
fin.close();
int colNum;
string colName = "LAST PERSON";
static int it;
for(vector<string>::iterator it = data[0].begin(); it != data[0].end(); ++it)
{
if …Run Code Online (Sandbox Code Playgroud) 以下代码编译良好,因为 g++ 允许它,但它会导致未定义的行为吗?或者我的代码可以正常工作?如果在使用时没有产生错误,那么 C++ 标准不允许可变长度数组是什么意思?
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
char abc[x];
cout << sizeof(abc) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ ×9
arrays ×6
algorithm ×1
blank-line ×1
c ×1
c++11 ×1
codeblocks ×1
constants ×1
dev-c++ ×1
g++ ×1
gcc ×1
nested-loops ×1
sizeof ×1
stl ×1