小编hkB*_*sai的帖子

如何使用两个参数使用overload()运算符; 喜欢(3,5)?

我有一个数学矩阵类.它包含一个成员函数,用于访问该类的任何元素.

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)

c++ operator-overloading matrix

5
推荐指数
1
解决办法
617
查看次数

警告:"对"D"及其转置的乘法进行Parethesize,以确保结果是Hermetian."

在此输入图像描述

正如您在上面的屏幕截图中看到的,我在Matlab m文件代码中有以下表达式:
K = P * D * D' * P;
其中,P是nxn矩阵,D是nx1列向量(n = 4,如果重要的话).

为什么我收到此警告消息?
如果我在那里使用或不使用括号会有什么变化?

matlab warnings matrix multiplication parentheses

5
推荐指数
1
解决办法
976
查看次数

什么时候可以在手机上托管网站并安装域名服务器?

我的问题是关于托管没有大量流量的小型个人网站。

我们(标准 PC 用户)为网络托管服务付费的最大原因是(如果我错了,请纠正我):

  1. 我们不会一直打开电脑。
  2. 我们中的一些/大多数人没有静态 IP 地址。

然而,现代手机只不过是一直保持打开状态的小型PC。如果它们也可以有静态 IP 地址,为什么我们不能在它们上面安装 Web 服务器?

目前有这项技术吗?
如果是,目前它的使用范围如何?而且,它什么时候才能战胜传统的远程付费网络服务器?如果没有,它将何时以及如何可用?我们需要使用什么硬件(手机类型)?

dns webserver web-hosting mobile-phones

5
推荐指数
1
解决办法
2995
查看次数

如何以正确的方式编写代码内注释和文档?这有什么标准吗?

我想通过注释行在我的代码中添加文档.
这有什么标准格式吗?

例如,请考虑以下代码:

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)

对于此示例代码,是否有更好的方法来编写注释行?

c++ documentation standards comments

4
推荐指数
2
解决办法
1501
查看次数

可变长度参数列表 - 如何理解我们检索到的最后一个参数?

我有一个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)

我知道我们通常会传递一个额外的参数来告诉收件人方法参数的总数,或者我们传递一个感性的结束参数.但为了保持简洁和干净,我不喜欢传递额外的参数.

有没有办法在不修改示例中的方法签名的情况下执行此操作?

c++ variadic-functions

4
推荐指数
1
解决办法
1685
查看次数

如何为非模板类​​定义模板方法?

我的编译器对我实现模板方法的方式不满意.它为这些实现提供了大量错误消息,如"未定义类型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)

c++ methods implementation templates visual-studio

4
推荐指数
1
解决办法
4940
查看次数

如何在不明确定义函数的情况下创建函数的std :: vector?

我想创建一个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

4
推荐指数
1
解决办法
803
查看次数

如何检查两个std :: vector是否只包含相同的元素?

我需要一个算法或标准库函数来比较两个向量元素,如下所示:

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)

c++ algorithm comparison stl vector

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

为什么`std :: string :: reserve()`没有保留我指定的确切空间量?

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)

c++ memory-management stl stdstring

3
推荐指数
2
解决办法
602
查看次数

是否可以用C++编写自定义转换操作符(如`static_cast`)?

我得到了这个想法并尝试编写一个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++中是不可能的,还是我在代码中遗漏了一些东西?

c++ casting

3
推荐指数
1
解决办法
918
查看次数