我试图通过使用两个索引将值添加到2D矢量.当我运行我的程序时,我收到Windows消息,说该程序已停止工作.使用Dev-C++进行调试表明存在分段错误(我不确定这意味着什么).请不要建议使用数组,我必须使用向量进行此分配.
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
vector< vector<int> > matrix;
cout << "Filling matrix with test numbers.";
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
matrix[i][j] = 5; // causes program to stop working
}
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个测试用例,我想在其中填充值为5的3X3矩阵.我怀疑它与2D矢量的大小没有特别定义有关.如何使用索引用值填充2D向量?
public static int[] uniqueRandomElements (int size) {
int[] a = new int[size];
for (int i = 0; i < size; i++) {
a[i] = (int)(Math.random()*10);
for (int j = 0; j < i; j++) {
if (a[i] == a[j]) {
a[j] = (int)(Math.random()*10);
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
return a;
}
Run Code Online (Sandbox Code Playgroud)
我有一个方法,应该生成用户指定的随机元素数组.随机生成的整数应介于0和10之间(包括0和10).我能够生成随机整数,但我遇到的问题是检查唯一性.我检查唯一性的尝试是在我上面的代码中,但数组仍然包含重复的整数.我做错了什么,有人能给我一个暗示吗?
我有一个任务,我应该读取包含整数的多个文件(每行一个),并在排序后将它们合并到输出文本文件中.我是C++的新手,所以我不知道一切是如何运作的.我用两个.txt文件测试我的程序.第一个文件名为fileone.txt,包含1,2,7(我不知道如何格式化它,但它们都在不同的行上.)第二个文件名为filetwo.txt,包含1,3,5, 9,10(同样每个整数都在不同的行上).
我编写了以下代码,打开两个文件并打印内容.
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv) {
ifstream iFile;
ifstream jFile;
iFile.open("fileone.txt");
jFile.open("filetwo.txt");
int int1 = 0;
int int2 = 0;
if (iFile.is_open() && jFile.is_open() ){
while (iFile.good() || jFile.good() ) {
iFile >> int1;
jFile >> int2;
cout << "From first file:" << int1 << endl;
cout << "From second file:" << int2 << endl;
}
}
iFile.close();
jFile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个程序的输出是

我遇到的问题是第一个文件中的最后一个数字被多次打印.我想要的输出是在打印文件中的最后一个整数后停止打印.仅当第二个文件包含的整数多于第一个文件时,才会出现此问题.有没有办法在第一个文件到达结尾时停止打印,同时仍然打印第二个文件中的所有数字?
我是从麻省理工学院开放课件网站自学Python.我只使用讲座中学到的信息完成这项任务时遇到了麻烦.我学到的最后一件事是使用"While"和"For"循环的迭代.我还没有学过功能.是否可以编写一个程序来计算和打印第1000个素数只用这个?
到目前为止,这是我的代码:
count = 0
prime = []
candidate = []
x = 2
y = 1
while count < 1000:
x = x+1
if x > 1:
if x%2 != 0:
if x%3 != 0:
if x%5 != 0:
if x%7 != 0:
if x%11 != 0:
if x%13 != 0:
candidate.append(x)
Run Code Online (Sandbox Code Playgroud) 我有这个程序显示每加仑汽油的成本.
System.out.printf("%10s %.2f %s %n", "At", cost , "per gallon,");
Run Code Online (Sandbox Code Playgroud)
输出显示
每加仑5.12,
但我想要它显示
每加仑5.12美元,
我试图在"At"之后添加$,但它在显示成本之前显示一个空格.有没有办法删除"$.之间的空格
我正在使用单独的编译进行家庭作业,我有一个关于访问我创建的类的数据成员的问题.当实现不带任何参数的类的成员函数并且我需要访问该类的数据成员时,我将如何在C++中执行此操作?我知道在Java中,有一个this关键字指的是调用该函数的对象.
我的头文件:
#ifndef _POLYNOMIAL_H
#define _POLYNOMIAL_H
#include <iostream>
#include <vector>
class Polynomial {
private:
std::vector<int> polynomial;
public:
// Default constructor
Polynomial();
// Parameterized constructor
Polynomial(std::vector<int> poly);
// Return the degree of of a polynomial.
int degree();
};
#endif
Run Code Online (Sandbox Code Playgroud)
我的实施文件:
#include "Polynomial.h"
Polynomial::Polynomial() {
// Some code
}
Polynomial::Polynomial(std::vector<int> poly) {
// Some code
}
int degree() {
// How would I access the data members of the object that calls this method?
// Example: polynomialOne.degree(), How would I …Run Code Online (Sandbox Code Playgroud)