在不使用任何外部库

时间:2016-09-08 23:45:45

标签: c++ arrays

我是C ++语法的初学者。现在,我需要在C ++中创建一个mxn 2D数组,以便在另一个项目中使用它。我查看了其他涉及使用vector等工具的答案。很多工具都没有在我的Visual Studio 15上工作,即对于vector,如果没有像std::vector这样的消息我无法定义vector is not in std。所以,我写了以下代码:

#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
    int i; int j; int row[5][10] = {};
    for (int j = 0; j < 10;)
        for (int i = 0; i < 5;)
        {
            row[i][j] = 500;
            int printf(row[i][j]);
            i++;
            j++;
            cout << "Array:" << row[i][j] << endl;
        }
    return 0;
}

当然,这不是正确的语法。所以输出超出了我的预期。我想创建一个m * n数组,其中所有元素都是相同的整数;在这种情况下为500。也就是说,如果m = 3,n = 2,我应该得到

500 500 500
500 500 500

3 个答案:

答案 0 :(得分:1)

您当前的代码存在一些问题。

  1. 第一个for循环缺少大括号
  2. 您正在for循环中重新定义int i和int j。这不是一个恭维问题,但仍然是一个问题。
  3. 您未正确使用printfprintf用于将字符串输出到控制台。正确的行是printf("%d", row[i][j]);
  4. 如果您想使用vector,则必须使用#include <vector>加入vector。您可以使用与数组非常相似的L.dat.comp <- combn(dat.example$L, 2, paste0, collapse = "_", simplify=F) master <- data.frame(cbind(calc.diff,unlist(L.dat.comp))) ,但不必担心大小问题。

答案 1 :(得分:1)

你似乎在学习。所以,我做了最小的修正,使其工作。我建议你根据自己的需要进行修改。

class simpleClass1
{
    public DateTime Date;
    public int AnotherField;
}

class simpleClass2
{
    public DateTime Date2;
    public int AnotherField;
}

public List<object> GetResultList()
{                           
    var listOfObjects = new List<object>{new simpleClass1 {Date = DateTime.MinValue, AnotherField = 2},new simpleClass2 {Date2 = DateTime.MinValue, AnotherField = 2}};
    return listOfSimpleClasses.Where(x =>
    {
        if (x is simpleClass2)
            return (x as simpleClass2).Date2 == DateTime.MinValue;
        if (x is simpleClass1)
            return (x as simpleClass1).Date == DateTime.MinValue;
        return false;
    }).ToList();
}

答案 2 :(得分:0)

使用OP程序护理和喂养var years = eu_popul.map(function(d) {return d.Land});作为例子。

std::vector

一个性能注释向量向量不提供一个长存储器块。它提供M + 1块内存,可以存储在任何地方。通常,当现代CPU从内存中读取值时,它还会读取它周围的值,假设如果您希望项目位于X,那么您可能希望不久之后的位置X + 1处的值。这允许CPU一次加载,高速缓存&#34;多个值。如果您必须通过内存跳转,这不起作用。这意味着CPU可能发现自己花费更多时间来检索矢量矢量的部分而不是处理矢量矢量。典型的解决方案是伪造具有1D结构的2D数据结构并自己执行2D到1D映射。

所以:

#include <iostream>
#include <vector> // needed to get the code that makes the vector work

int main()
{
    int m, n; // declare m and n to hold the dimensions of the vector

    if (std::cin >> m >> n) // get m and n from user
    { // m and n are good we can continue. Well sort of good. The user could
      // type insane numbers that will explode the vector, but at least they
      // are numbers.

      // Always test ALL user input before you use it. Users are malicious
      // and incompetent <expletive delteted>s, so never trust them.

      // That said, input validation is a long topic and out of scope for this
      // answer, so I'm going to let trapping bad numbers pass in the interests
      // of keeping it simple

        // declare row as a vector of vectors
        std::vector<std::vector<int>> row(m, std::vector<int> (n, 500));

        // breaking this down:
        // std::vector<std::vector<int>> row
        //      row is a vector of vectors of ints
        // row(m, std::vector<int> (n, 500));
        //      row's outer vector is m std::vector<int>s constructed with
        //      n ints all set to 500

        for (int j = 0; j < n; j++) // note: j++ has been moved here. This is
                                     // exactly what the third part of a for
                                     // statement is for. Less surprises for
                                     // everyone this way
        // note to purists. I'm ignoring the possible advantages of ++j because
        // explaining them would muddy the answer.
        // Another note: This will output the transverse of row because of the
        // ordering of i and j; 
        {
            for (int i = 0; i < m; i++) // ditto I++ here
            {
                // there is no need to assign anything here. The vector did 
                // it for us
                std::cout << " " << row[i][j]; // moved the line ending so that
                                               // the line isn't ended with
                                               // every column
            }
            std::cout << '\n'; // end the line on the end of a row
            // Note: I also changed the type of line ending. endl ends the line
            // AND writes the contents of the output stream to whatever media
            // the stream represents (in this case the console) rather than
            // buffering the stream and writing at a more opportune time. Too
            // much endl can be a performance killer, so use it sparingly and
            // almost certainly not in a loop
        }
        std::cout << std::endl; // ending the line again to demonstrate a better
                                // placement of endl. The stream is only forced
                                // to flush once, right at the end of the
                                // program
                                // even this may be redundant as the stream will
                                // flush when the program exits, assuming the
                                // program does not crash on exit.
    }
    else
    { // let the use know the input was not accepted. Prompt feedback is good
      // otherwise the user may assume everything worked, or in the case of a
      // long program, assume that it crashed or is misbehaving and terminate
      // the program.
        std::cout << "Bad input. Program exiting" << std::endl;
    }
    return 0;
}

看起来好多了,是吗?访问看起来有点丑陋,但

std::vector<int> row(m*n, 500); 

有趣的是,将std::cout << " " << row[i * n + j]; 转换为内存地址的幕后工作几乎与row[j][i]相同,所以即使你显示更多工作,它也不会# 39; t 再取。除此之外,您从CPU成功预测和提前读取的好处中,您的程序通常会快得多。