小编Kon*_*lph的帖子

如何使用调用函数的对象的参数?

//Function to find the distance between 2 points
TLength CPoint::Distance(const CPoint& point) const
{
    TLength horiz = abs(point.X() - OBJECT.X());
  TLength vert = abs(point.Y() - OBJECT.Y());
  TLength dist = sqrt(pow(horiz,2) + pow(vert,2)); 
  return dist;
};


int main()
{
const CPoint a = CPoint(4,5);
const CPoint b = CPoint(1,1);

a.Distance(b);

};
Run Code Online (Sandbox Code Playgroud)

是否有一个术语可以代替 OBJECT 来使用函数 Distance 中的 a 值?

c++ function

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

BigInteger.intValue() 在 C# 中等效

我正在尝试将 Java 代码转换为 C#,但在处理 BigInteger 操作时遇到了问题。我找到了一些关于 C# 中 BigInteger 实现和 intValue 本身的资源。但是没有关于 C# 中 BigInteger.intValue 等价物的线索。Java中的定义是:

将此 BigInteger 转换为 int。这种转换类似于 Java™ 语言规范第 5.1.3 节中定义的从 long 到 int 的缩小原始转换:如果此 BigInteger 太大而无法放入 int,则仅返回低 32 位。注意此转换可能会丢失有关 BigInteger 值的整体大小的信息,并会返回具有相反符号的结果

但是在 C# 中使用 (int) 获得类似的结果会导致错误:

对于 Int32,值要么太大要么太小。

我也尝试只使用低字节但没有成功如果有人帮忙,我们将不胜感激

c# java biginteger

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

重载函数调用运算符 () 用于模板对象的索引和赋值

我正在尝试为矩阵数据结构创建模板,并且我希望以简洁直观的方式来索引和分配元素(即 'A(i,j)' 返回一个元素和 'A(i,j)=x ' 为该元素分配一个值。)

根据其他论坛主题,我看到通过引用返回数组元素,函数/运算符可以以这种方式返回和更改该元素。

template <typename T, int Rows, int Cols>
    struct Matrix {
        private:
        public:
            const int rows = Rows; //number of rows
            const int cols = Cols; //number of columns
            T data[Rows*Cols];     //contents of matrix

            //Single element indexing and assigment
            T& operator() (int i, int j) {
                return data[ (i-1)*(this->cols) + j ];
            }
    };

int main(){
    const int m = 3;
    const int n = 4;
    Matrix<float, m, n> A;

    for (int i = …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading

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

为什么“new”的不同使用方式会导致结果与“delete”不兼容?

我正在阅读有关内存的 C++ Primer Plus, 12.1.3,以及有关析构函数的一些内容真的让我感到困惑。

//Here is a default construtor of String class
String::String()
{
   len = 0;
   str = new char[1];
   str[0] = '\0';
}
Run Code Online (Sandbox Code Playgroud)

书上说,使用str = new char[1]not str = new char,两种方式分配相同的内存,但第二种与析构函数不兼容。此外,书中说以下3种方式不好,因为它们与“删除”不兼容

char words[15] = "bad idea";
char *p1 = words;
char *p2 = new char;
char *p3;
delete [] p1; //undefined, so don't do it
delete [] p2; //undefined, so don't do it
delete [] p3; //undefined, so don't do it
Run Code Online (Sandbox Code Playgroud)

我不知道这 3 种方式有什么不同,有人可以向我解释一下吗?非常感谢。

c++ pointers destructor class

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

使用 DataCatalog 保存数据

我在看iriskedro 提供的项目示例。除了记录准确性之外,我还想将predictions和保存test_y为 csv。

这是 kedro 提供的示例节点。

def report_accuracy(predictions: np.ndarray, test_y: pd.DataFrame) -> None:
    """Node for reporting the accuracy of the predictions performed by the
    previous node. Notice that this function has no outputs, except logging.
    """
    # Get true class index
    target = np.argmax(test_y.to_numpy(), axis=1)
    # Calculate accuracy of predictions
    accuracy = np.sum(predictions == target) / target.shape[0]
    # Log the accuracy of the model
    log = logging.getLogger(__name__)
    log.info("Model accuracy on test set: %0.2f%%", accuracy …
Run Code Online (Sandbox Code Playgroud)

python kedro

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

格式参数太多 [-Wformat-extra-args] 和另一个

double drake_equation(double RofSF, double FSwP, int NofPSL, double PDL, double PwIL, double CwDC, int LofTRS);\n\nint main(int argc, char **argv)\n{\n  double RofSF[] = {1.0, 1.0, 1.5, 3.0};\n  double FSwP[] = {0.2, 0.5, 1.0, 1.0};\n  int NofPSL[] = {1, 5, 3, 5};\n  double PDL[] = {1.0, 1.0, 1.0, 1.0};\n  double PwIL[] = {1.0, 1.0, 0.0001, 1.0};\n  double CwDC[] = {0.1, 0.2, 0.001, 0.1};\n  int LofTRS[] = {1000, 1000000000, 304, 10000000};\n  double N;\n  int i;\n  for (i=1; i< 5; i++)\n  {\n    N = …
Run Code Online (Sandbox Code Playgroud)

c

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

std:atomic 使用引用初始化会创建基础变量的副本吗?

让我们考虑下一个片段:

int val=5;
int& ref=val;
std::atomic<int> atomicref(ref);

++atomicref;
std::cout<< "atomic ref="<<atomicref.load()<<" original ref="<<ref<<" original val="<<val;
Run Code Online (Sandbox Code Playgroud)

当我在 Mac OS X、XCode 8.3.3、c++11 下编译它时,我收到如下输出:

atomic ref=6 original ref=5 original val=5
Run Code Online (Sandbox Code Playgroud)

该行:

std::atomic<int> atomicref(ref);

当然看起来很可疑,因为原子下的类型与变量声明中的类型不同 - 它是引用。

我想知道为什么这些值不匹配;说atomicref实际上创建了val的副本是否正确?

c++ macos std c++11 stdatomic

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

创建一个函数来识别缺失值

我正在尝试构建一个函数作为 R 中更大函数的一部分。有些部分工作正常,但其他部分则不然。这是给我带来问题的代码片段。

这部分函数旨在识别数据框中的变量是否丢失,然后生成一个新变量来记录该特定情况是否丢失或存在。我希望新变量具有后缀 .zero(q1 变为 q1_zero,q2 变为 q2_zero 等)。我可以毫无问题地生成后缀。创建新变量会导致一些问题。任何见解将不胜感激。

function1 <- function (x, data) {
  # new variable name
  temp <- paste (x, .zero, sep="", collapse = NULL)
  temp
  
  # is variable missing
  # I don't know if I should use this method or ifelse()
  data$temp [is.na (data$x)]<- 0
  data$temp [!is.na (data$x)]<- 1
 return (data$temp)
  }
Run Code Online (Sandbox Code Playgroud)

variables r function missing-data

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

检查标志是否包含其他标志的任何值

我想比较两个标志,看看它们是否有共同的值。
我希望有一个扩展方法,以便“加速”编码(我将经常在各种枚举类型上使用它)。我怎么能够?
这段代码告诉我:

运算符“&”不能应用于 enum 和 enum 类型的操作数

public enum Tag
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 4,
    Value4 = 8,
    Value5 = 16
}

public static class FlagExt
{
    public static bool HasAny(this Enum me, Enum other)
    {
        return (me & other) != 0;
    }
}

public class Program
{
    public static void Main()
    {
        Tag flag1 = Tag.Value1 | Tag.Value2 | Tag.Value3;
        Tag flag2 = Tag.Value2 | Tag.Value3 | Tag.Value4;
        
        Console.WriteLine(flag1.HasAny(flag2)); // it should returns …
Run Code Online (Sandbox Code Playgroud)

c# enums enum-flags

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

向量上可以进行二分查找吗?

我知道二分搜索适用于排序数组,因为由于数组索引,可以在单位时间内访问中间元素。但在列表中,访问中间元素需要线性时间,使得二分搜索毫无意义。向量具有像列表一样灵活的大小,因此如果使用列表实现它们,则二分搜索不应该对它们起作用,对吗?或者向量是否使用具有动态内存分配的数组,并且在这种情况下二分搜索是否有效?(我是个初学者,逻辑上的错误还请大家指出)

c++ arrays search vector binary-search

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