【问题标题】:Finding out character 'a' from a file then count the lines in which the character is appeared [duplicate]从文件中找出字符“a”,然后计算出现该字符的行数[重复]
【发布时间】:2018-02-04 06:00:22
【问题描述】:

不知道该怎么做。尝试了类似下面的方法。希望在代码中进行更多优化。
Everyting 应该只在一个函数中,
指导我如何打开关闭文件,
如何在每一行中查找字符,
增加计数器。

void simpleFileIn(void) {

    string line;
    ifstream myfile("example.txt");
    if (myfile.is_open()) {
        while (getline(myfile, line)) {
            //found(line);
            size_t size = strlen(line);
            cout << line << '\n';
        }
        myfile.close();
    }
    else
        cout << "Unable to open file";
}

函数 simpleFileIn() 应该可以工作,打开文件然后在工作完成后关闭。
找出字符 a 并计算整数。 想要关闭/删除这个问题,因为我被禁止要求更多帮助我。情况一天比一天糟糕

【问题讨论】:

  • 问题是什么?
  • 请说明您认为这条线的作用*pptr=ptr;。尤其是它对pptr[0]pptr[1] 的影响有什么区别。
  • 强制“使用std::vector而不是原始数组”
  • 我不确定有多少人在问“你到底在问什么”。对我来说,这似乎很清楚:OP 想要使用他动态分配的 2D 数组,但发现他不能。 问题是:为什么? 回答:他分配错了:查看此线程上的所有现有答案。
  • @Askish Kamble 这里不允许更改问题(尤其是在有答案并且您接受一个答案之后)。对于新问题,创建新问题(线程)。谢谢。

标签: c++ pointers


【解决方案1】:

您需要在循环中分配行:

int** pptr = new int* [rows]; // <<== rows, not cols
for(int i=0;i<rows;i++){
    pptr[i] = new int[cols]; // <<== Add this line
    for(int j=0;j<cols;j++){
        cout<<"Enter value at "<<i<<j<<endl;
        cin>>pptr[i][j];
        cout<<"Value is "<<pptr[i][j]<<endl;
    }
}

在删除指向它们的指针数组之前,您还需要删除单个行。使用带方括号的delete[] 运算符:

for(int i=0;i<rows;i++){
    delete[] pptr[i];
}
delete[] pptr;

您不需要将NULLs 分配给已删除的指针,除非您打算稍后将指针用于其他用途。

【讨论】:

    【解决方案2】:

    您分配的指针数组错误。

    首先你必须为row指针分配足够的空间

    int** pptr = new int* [rows];
    

    每个指针都有足够的空间存放col 整数

    for (int i = 0; i < cols; i++)
    {
        pptr[i] = new int [cols];
    }
    

    要删除数组,请使用delete[] 而不是delete

    删除每一行

    for (int i = 0; i < rows; i++)
    {
        delete [] pptr[i];
    }
    

    然后删除指针数组

    delete [] pptr;
    

    没有必要将NULL 分配给已删除的指针,因为您不会再次使用它们。同样在 中,您应该使用nullptr 而不是NULL

    Here is the correct using of array of pointers.


    你的错误

    int* ptr = new int [rows];
    int** pptr = new int* [cols];
    *pptr=ptr;
    
    • 交换行和列
    • 仅为第一个指针/行分配内存,其他未初始化 -> UB
    • 使用delete 而不是delete[]

    【讨论】:

      【解决方案3】:

      所以分配似乎有些混乱。来自您的代码

      int* ptr = new int [rows];
      int** pptr = new int* [cols];
      *pptr=ptr;
      

      您现在已经创建了一维数组。然后,您取消引用 pptr 并分配给它 ptr 这与

      pptr[0] = ptr;
      

      所以你只是在初始化第一列。您想将此代码更改为

      int** pptr = new int* [cols];
      for (int i = 0; i < cols; ++i) {
          pptr[i] = new int [rows];
      }
      

      这将正确分配内存

      【讨论】:

        【解决方案4】:

        您可以为您的 2D 数组创建一个排序构造函数,以便您拥有单行簿记:

        #include <iostream>
        
        template <typename T>
        T** new_( std::size_t rows, std::size_t columns )
        {
          auto dsize = rows    * sizeof(T*);
          auto rsize = columns * sizeof(T);
          auto tsize = rows    * rsize;
        
          unsigned char* data = new unsigned char[ dsize + tsize ];
          T** result = (T**)data;
          T*  table  = (T*)(data + dsize);
        
          while (rows--) 
            result[ rows ] = table + rows * columns;
        
          return result;
        }
        
        int main()
        {
          int m; std::cout << "m? ";  std::cin >> m;
          int n; std::cout << "n? ";  std::cin >> n;
        
          // Create the new matrix
          int** a = new_<int>( m, n );
        
          // Do stuff with a[ r ][ c ] here.
          // It looks and behaves JUST LIKE a normal 2D C array
          // in all respects EXCEPT one: &a != &(a[0][0]).
          // Use the latter when passing to a flat function!
        
          // Delete it
          delete [] a;
        }
        

        享受诡异。

        【讨论】:

          猜你喜欢
          • 2012-05-31
          • 2011-03-02
          • 2020-06-14
          • 1970-01-01
          • 2013-12-23
          • 1970-01-01
          • 2013-11-04
          相关资源
          最近更新 更多