我有一个2D char矢量:
vector< vector<char> > matrix;
Run Code Online (Sandbox Code Playgroud)
我将在矩阵中读取输入并将其存储在该向量中.我的矢量大小是固定的,是ROW x COL.我想我需要为每一行和每列调整大小.
如何在不占用额外内存(正确调整大小)的情况下完成它?
我是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) 我想知道以下两个函数中的哪一个在时间和空间方面最有效.它们都检查堆栈中是否存在某个元素.第一个使用按值传递机制,而第二个使用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)
}
我在理解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)