小编use*_*007的帖子

如何调整2D C++向量的大小?

我有一个2D char矢量:

vector< vector<char> > matrix;
Run Code Online (Sandbox Code Playgroud)

我将在矩阵中读取输入并将其存储在该向量中.我的矢量大小是固定的,是ROW x COL.我想我需要为每一行和每列调整大小.

如何在不占用额外内存(正确调整大小)的情况下完成它?

c++ resize vector

7
推荐指数
1
解决办法
2万
查看次数

在另一个类构造函数中初始化一个类对象

我是C++的新手.好吧,我有box.cpp和circle.cpp文件.在我解释我的问题之前,我想给你他们的定义:

在box.cpp中

  class Box
  {
       private:
       int area;

       public:
       Box(int area);
       int getArea() const;

  }
Run Code Online (Sandbox Code Playgroud)

在circle.cpp中

   #include "box.h"
   class Circle
   {
      private:
      int area;
      Box box;

      public:
      Circle(int area, string str);
      int getArea() const;
      const Box& getBoxArea() const;  

   }
Run Code Online (Sandbox Code Playgroud)

现在你可以在Circle类中看到我有一个整数值和Box对象.在Circle构造函数中,我可以轻松地将整数值分配给区域.

一个问题是我被赋予了一个字符串,用于将其分配给Box对象

所以我在Circle构造函数中做的是:

 Circle :: Circle(int area, string str)
 {
  this->area = area;
  // here I convert string to an integer value
  // Lets say int_str;
  // And later I assign that int_str to Box object like this:
    Box box(int_str);

 } …
Run Code Online (Sandbox Code Playgroud)

c++ class

5
推荐指数
2
解决办法
2万
查看次数

c ++中pass-by-value和pass-by-reference之间的差异

我想知道以下两个函数中的哪一个在时间和空间方面最有效.它们都检查堆栈中是否存在某个元素.第一个使用按值传递机制,而第二个使用pass-by-reference.我可能错了,但我认为pass-by-value机制隐式复制参数,而在pass-by-ref中我们明确地做了.

第一版按值传递:

 template<class T>
 bool find (stack<T> source, T value)
{
    while (!source.isEmpty() && source.top() != value)
        source.pop();

    if (!source.isEmpty())
        return true;

    return false;
}
Run Code Online (Sandbox Code Playgroud)

第二版通过引用传递:

template<class T>
bool find (const stack<T> &source, T value)
{
 stack<T> temp = source; 
while (!temp.isEmpty() && temp.top() != value)
    temp.pop();

if (!temp.isEmpty())
     return true;

return false;
Run Code Online (Sandbox Code Playgroud)

}

c++ pass-by-reference pass-by-value

0
推荐指数
1
解决办法
2367
查看次数

识别haskell类型

我在理解haskell中的类型方面遇到了一些困难.让我们考虑以下函数并查看它们的类型.

reduce f s [] = s
reduce f s (x:xs) = f x (reduce f s xs)

for m n f s = if m>n then s else for (m+1) n f ( f m s )

comp f g x y = f x (g x y)

iter 0 f s = s
iter n f s = iter (n-1) f (f s)
Run Code Online (Sandbox Code Playgroud)

我们有类似的东西:

reduce :: (t1 -> t -> t) -> t -> [t1] -> t
for …
Run Code Online (Sandbox Code Playgroud)

haskell types functional-programming

0
推荐指数
1
解决办法
95
查看次数