我有一个包含二维数组的类。
int arr[3][3];
Run Code Online (Sandbox Code Playgroud)
我将如何将该矩阵存储在 std::list 中?
list<*> explored;
explored.pushback(classname->arr);
Run Code Online (Sandbox Code Playgroud)
我想也许是因为我已经知道数组的大小,所以我只会创建一个具有类似上述内容的指针列表,但这显然不起作用。我将如何初始化列表?我将如何单独访问二维数组?
编辑:我想要一个包含多个二维数组的列表。这意味着每个索引位置将保存一个数组。为了解决我的问题,我决定创建一个类,让这个类拥有一个矩阵。然后我会简单地通过做类似的事情来获得矩阵
Class Node{
Int matrix[3][3];
}
//Store a node with a matrix inside of it.
list<node> v;
v.pushback(node);
//retrieve the matrix by iterating to that position in the list then
v.matrix;
Run Code Online (Sandbox Code Playgroud) 这是一个简单的家庭作业,过去几天一直让我发疯.如果我要使用几个阵列,我可以在不久前完成它但是必须使用StringTokenizer让我疯了.
我遇到的主要问题是读取CSV文件.我不知道该怎么做,之前的在线搜索只提出了超级强烈的解决方案,这对于像我这样的初学者来说太过分了.
这是我的代码.如你所见,我不知道是否使用.nextLine()
或.NextToken()
.似乎都没有用.
对于那些想知道作业的人来说,基本上是用逗号分隔前4个产品,然后读取其余行作为这4个产品的评级.基本上6行,4列.第一行是产品,其余的是评级.
import java.util.StringTokenizer;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ProductRating {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner fileIn=null;
try{
fileIn = new Scanner(
new FileInputStream("C:/Users/Cristian/Desktop"));
}
catch (FileNotFoundException e)
{ // This block executed if the file is not found
// and then the program exits
System.out.println("File not found.");
System.exit(0);
}
//If Opened File Successful this runs
String products = "A";
String rantings ="0";
System.out.println("Gathering …
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种优雅的方法来确定哪个元素在C++ ptr数组中出现次数最多(模式).
例如,在
{"pear", "apple", "orange", "apple"}
Run Code Online (Sandbox Code Playgroud)
"apple"
元素是最常见的元素.
我以前的尝试失败编辑:数组已经排序.
int getMode(int *students,int size)
{
int mode;
int count=0,
maxCount=0,
preVal;
preVal=students[0]; //preVall holds current mode number being compared
count=1;
for(int i =0; i<size; i++) //Check each number in the array
{
if(students[i]==preVal) //checks if current mode is seen again
{
count++; //The amount of times current mode number has been seen.
if(maxCount<count) //if the amount of times mode has been seen is more than maxcount
{
maxCount=count; //the larger …
Run Code Online (Sandbox Code Playgroud)