문제

If I have the following Java code:

int[][] readAPuzzle()
{
    Scanner input = new Scanner(System.in);

    int[][] grid = new int[9][9];
    for (int i=0; i<9; i++)
      for (int j=0; j<9; j++)
        grid[i][j] = input.nextInt();

    return grid;
}

public static void main(String[] args) {
    // Read a Sudoku puzzle
    int[][] grid = readAPuzzle();
}

How can I convert this to C++? I get hung up on passing the array. Here is my attempt:

#include <iostream>

using namespace std;

const int puzzle_width = 9;
const int puzzle_height = 9;

void readAPuzzle(int (&grid)[puzzle_height][puzzle_width])
{
    for(int i = 0; i < 9; i++)
        for(int j = 0; j < 9; j++)
            grid[i][j] = cin >> grid[i][j];

    return;
}

int main()
{
    int[9][9] grid;
    readAPuzzle(grid);
}

What am I doing wrong in general?

도움이 되었습니까?

해결책

You need to read in the input text into your array grid and pass it on.

grid[i][j] = cin >> grid[i][j];

Doesn't do what you think it does, it tries to assign an object of type istream to grid[ i ][ j ]

cin >> grid[i][j]; 

however suffices.

Also, note in C++ the dimensions follow the identifier as in:

int grid[9][9];

다른 팁

Try

#include <iostream>
using namespace std;

const int puzzle_width = 9;
const int puzzle_height = 9;

void readAPuzzle(int grid[puzzle_height][puzzle_width])
{
    for(int i = 0; i < 9; i++)
        for(int j = 0; j < 9; j++)
            cin >> grid[i][j];
}

int main()
{
    int grid[9][9];
    readAPuzzle(grid);
}

In general, arrays are automatically passed by reference, and array sizes go after the name of the array not after their type.

And if you declared constants, you should always use puzzle_width and puzzle_height (perhaps shorten their names though) and not magic numbers like 9.

The simple answer is to use vectors instead of arrays. C++'s rules for passing arrays as function parameters are esoteric and derived from C. Here are some of the issues:

  • You can't use arrays for long without understanding and using pointers

Array subscripting is pointer subscripting. Arrays are accessed using pointer arithmetic. Arrays as function parameters are actually pointers in disguise.

  • Functions don't get information about array size when taking an array argument

Consider the declaration:

void inc_all(int myarray[]); /* increments each member of the array */

Unfortunately, that array parameter is not an array parameter! It's actually a pointer parameter:

void inc_all(int *myarray); /* Exactly the same thing! */

And a pointer doesn't know how many items are in the sequence it points at. As a result this function cannot have the information necessary to know when the array stops. You either need to pass the length:

void inc_all(int *myarray, size_t len); /* size_t rather than int */

or you need to use a sentinel value to mark the end of the array. Either way, an array is not a self-contained encapsulated datatype like a vector is.

  • You can't pass an arbitrarily-sized two-dimensional array to a function

If you try to create a function which takes a two-dimensional array:

void inc_all(int myarray[][]); /* XXX won't compile! */

it won't compile. The problem is you have an indeterminate length array of indeterminate length arrays of ints. The outer array doesn't know how large its members (the inner arrays) are and therefore doesn't know how to step through them in memory. You need to specify the size of the inner arrays:

void inc_all(int myarray[][10]);

at which point your code is probably not as general as you were hoping it was going to be.

If you use vectors and vectors of vectorss, these problems don't arise because the vectors themselves know how many members they have and carry that information with them.

If you still want to learn more about arrays and pointers I recommend section 6 of the comp.lang.c FAQ.

라디오 버튼에서 이벤트 선택을 선택하고 사용자가 라디오 버튼에서 값을 선택할 때 이벤트를 추가해야합니다.HTML 콘텐츠의 모든 항목을 가져 와서 생성 된 날짜를 구문 분석하는 JS 함수를 만들 수 있습니다.그 후에는 그걸 현재의 날과 비교하여 지난 30 일 또는 90 일 동안 필터링 할 수 있습니다. 참고 : 아래와 같이 각 항목에 대해 고유 한 CSS 클래스를 사용해보십시오.

<div class=”lang-item-node” createddate=”2014-01-22 13:02:52” language=”French”>Content</div>
<div class=”lang-item-node” createddate=”2014-01-28 13:02:52” language=”German”>Content</div>
<div class=”lang-item-node” createddate=”2013-12-18 13:02:52” language=”Italian”>Content</div>
.

의사 코드 : (해당하는 경우 이들을 수정하십시오)

$('#radioButton').on('change', function(){
var filteredValue = $(this).val();
    var listItems = $('.lang-item-node');
    if(listItems && listItems.length > 0)
{
   $.each(listItems, function(){
    var createdDate = $(this).attr('createddate');
    //compare to your filtered condition 
    //if true: $(this).show();
    //else: $(this).hide();
});
}
});
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top