我有一个数学矩阵类.它包含一个成员函数,用于访问该类的任何元素.
template<class T>
class Matrix
{
public:
// ...
void SetElement(T dbElement, uint64_t unRow, uint64_t unCol);
// ...
};
template <class T>
void Matrix<T>::SetElement(T Element, uint64_t unRow, uint64_t unCol)
{
try
{
// "TheMatrix" is define as "std::vector<T> TheMatrix"
TheMatrix.at(m_unColSize * unRow + unCol) = Element;
}
catch(std::out_of_range & e)
{
// Do error handling here
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的代码中使用此方法,如下所示:
// create a matrix with 2 rows and 3 columns whose elements are double
Matrix<double> matrix(2, 3);
// change the value …Run Code Online (Sandbox Code Playgroud) 
正如您在上面的屏幕截图中看到的,我在Matlab m文件代码中有以下表达式:
K = P * D * D' * P;
其中,P是nxn矩阵,D是nx1列向量(n = 4,如果重要的话).
为什么我收到此警告消息?
如果我在那里使用或不使用括号会有什么变化?
我的问题是关于托管没有大量流量的小型个人网站。
我们(标准 PC 用户)为网络托管服务付费的最大原因是(如果我错了,请纠正我):
然而,现代手机只不过是一直保持打开状态的小型PC。如果它们也可以有静态 IP 地址,为什么我们不能在它们上面安装 Web 服务器?
目前有这项技术吗?
如果是,目前它的使用范围如何?而且,它什么时候才能战胜传统的远程付费网络服务器?如果没有,它将何时以及如何可用?我们需要使用什么硬件(手机类型)?
我想通过注释行在我的代码中添加文档.
这有什么标准格式吗?
例如,请考虑以下代码:
class Arithmetic
{
// This method adds two numbers, and returns the result.
// dbNum1 is the first number to add, and dbNum2 is second.
// The returning value is dbNum1+dbNum2.
static double AddTwoNumbers(double dbNum1, double dbNum2);
}
Run Code Online (Sandbox Code Playgroud)
对于此示例代码,是否有更好的方法来编写注释行?
我有一个Polynomial包含给定多项式系数的类.
它的一个重载构造函数应该通过变量参数列表接收这些系数.
template <class T>
Polynomial<T>::Polynomial(T FirstCoefficient, ...)
{
va_list ArgumentPointer;
va_start(ArgumentPointer, FirstCoefficient);
T NextCoefficient = FirstCoefficient;
std::vector<T> Coefficients;
while (true)
{
Coefficients.push_back(NextCoefficient);
NextCoefficient = va_arg(ArgumentPointer, T);
if (/* did we retrieve all the arguments */) // How do I implement this?
{
break;
}
}
m_Coefficients = Coefficients;
}
Run Code Online (Sandbox Code Playgroud)
我知道我们通常会传递一个额外的参数来告诉收件人方法参数的总数,或者我们传递一个感性的结束参数.但为了保持简洁和干净,我不喜欢传递额外的参数.
有没有办法在不修改示例中的方法签名的情况下执行此操作?
我的编译器对我实现模板方法的方式不满意.它为这些实现提供了大量错误消息,如"未定义类型T".
这是我的第一个方法,它是在类块之外实现的:
class VectorConvertor
{
public:
// ...
template <class T>
static void ReverseVectorElements(std::vector<T> & Vector);
// ...
};
template <class T>
void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector)
{
std::vector<T>::size_type size = Vector.size();
T swap;
for (std::vector<T>::size_type i=0; i<size/2; i++)
{
swap = Vector.at(i);
Vector.at(i) = Vector.at(size-1-i);
Vector.at(size-1-i) = swap;
}
}
Run Code Online (Sandbox Code Playgroud)
另一个是这个; 这次方法是在类中实现的:
class ElementaryMath
{
public:
// ...
template <class T> static char sign(T num)
{
return (num >= static_cast<T>(0)) ? static_cast<char>(+1) : static_cast<char>(-1);
}
// ...
} …Run Code Online (Sandbox Code Playgroud) 我想创建一个std :: vector对象(或任何其他标准或自定义容器类型),其中包含自定义和任意函数的元素,这些函数的签名都是相同的.
它应该是这样的:
// Define the functions and push them into a vector
std::vector<????> MyFunctions;
MyFunctions.push_back(double(int n, float f){ return (double) f / (double) n; });
MyFunctions.push_back(double(int n, float f){ return (double) sqrt((double) f) / (double) n; });
// ...
MyFunctions.push_back(double(int n, float f){ return (double) (f * f) / (double) (n + 1); });
// Create an argument list
std::vector<std::pair<int, float>> ArgumentList;
// ...
// Evaluate the functions with the given arguments
// Suppose that it is guarantied …Run Code Online (Sandbox Code Playgroud) c++ functional-programming function-pointers vector function-call
我需要一个算法或标准库函数来比较两个向量元素,如下所示:
class Utility
{
template <class T>
static bool CheckIfVectorsEquivalent( const std::vector<T> & Vec1,
const std::vector<T> & Vec2)
{
// ???
}
};
Run Code Online (Sandbox Code Playgroud)
按照以下规格工作:
std::vector<int> v1, v2, v3, v4, v5, v6, v7, v8;
// Returns false when not all the elements are matching between vectors
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v2.push_back(2);
v2.push_back(3);
v2.push_back(8);
Utility::CheckIfVectorsEquivalent(v1, v2); // Must return false
// Returns true when all the elements match, even if the are not in the same order
v3.push_back(3);
v3.push_back(1);
v3.push_back(7);
v4.push_back(7);
v4.push_back(3);
v4.push_back(1); …Run Code Online (Sandbox Code Playgroud) std::string::reserve()不会分配我作为参数传递的确切空间量.例如,如果我尝试为100个字符保留空间,则保留111个字符.如果我通过200,它预留207. 655,650,1007 1000.
这背后的原因是什么?
程序代码:
std::string mystr;
std::cout << "After creation :" << mystr.capacity() << std::endl;
mystr.reserve(1000);
std::cout << "After reserve() :" << mystr.capacity() << std::endl;
mystr = "asd";
std::cout << "After assignment :" << mystr.capacity() << std::endl;
mystr.clear();
std::cout << "After clear() :" << mystr.capacity() << std::endl;
Run Code Online (Sandbox Code Playgroud)
代码输出:
After creation :15
After reserve() :1007
After assignment :1007
After clear() :1007
Run Code Online (Sandbox Code Playgroud)
(IDE:Visual Studio 2012)
我得到了这个想法并尝试编写一个string_cast转换运算符来在C++字符串之间进行转换.
template <class OutputCharType>
class string_cast
{
public:
template <class InputCharType>
operator std::basic_string<OutputCharType>(const std::basic_string<InputCharType> & InputString)
{
std::basic_string<OutputCharType> OutputString;
const std::basic_string<InputCharType>::size_type LENGTH = InputString.length();
OutputString.resize(LENGTH);
for (std::basic_string<OutputCharType>::size_type i=0; i<LENGTH; i++)
{
OutputString[i] = static_cast<OutputCharType>(OutputString[i]);
}
return OutputString;
}
};
Run Code Online (Sandbox Code Playgroud)
我试着像这样使用它:
std::string AString("Hello world!");
std::cout << AString << std::endl;
std::wcout << string_cast<wchar_t>(AString) << std::endl; // ERROR
Run Code Online (Sandbox Code Playgroud)
错误消息是:
Error C2440 '<function-style-cast>': cannot convert from
'const std::string' to 'string_cast<wchar_t>'
Run Code Online (Sandbox Code Playgroud)
这在C++中是不可能的,还是我在代码中遗漏了一些东西?