小编Blo*_*nat的帖子

C - 如果数组中的值

我是 C 新手,目前正在研究数组。我想检查某个值是否在数组中,但我遇到了一些问题(不仅在语法方面,而且在理解方面)。

我的目标是有一个我可以调用的函数,给它两个参数——要搜索的值和要搜索的数组——并根据它是否被找到返回一个 0 或 1。

我的方法如下:

#include <stdio.h>
#include <stdlib.h>

int valueinarray(float val, float *arr[]);

int main()
{
    float arr[] = {5, 4.5, 4, 3.5, 3, 2.5,};
    int test = valueinarray(4.5, *arr[]);
    printf("%d", test);

    return 0;
}

int valueinarray(float val, float *arr[]){
    int i;
    for(i = 0; i < sizeof(*arr[]); i++){
        if(*arr[i] == val) return 1;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我现在有两个问题,特别是关于语法的问题:

  1. 如果我创建一个带有指针作为其参数之一的函数,我是否必须一直在函数内部使用“*arr[]”来引用它?还是“arr[]”甚至“arr”就足够了?

  2. 我是否正确理解我无法将整个数组传递给函数所以我改用指针?

此外,我的方法是错误的,我不明白为什么。迭代数组似乎工作得很好,甚至检查某个值是否在其中也有效,问题似乎出在我调用函数的方式上。我读过双指针,这是需要它们的场景吗?如果不是,他们需要什么?

非常感谢。

c arrays syntax

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

子组件中的ExpressionChangedAfterItHasBeenCheckedError

我有一个父组件,它每秒更新其数组myValue。在子组件中,我想创建一个图表,该图表使用此数组作为数据,并在每次父组件更新时也进行更新。

当我运行此应用程序时,出现以下错误:

错误:ExpressionChangedAfterItHasBeenCheckedError:检查表达式后,表达式已更改。先前的值:“ hidden:true”。当前值:“隐藏:假”。

这是我的父组件:

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.scss']
})
export class ParentComponent implements AfterContentInit, OnDestroy {

    private myValues: MyValue[];
    private alive: boolean;

    constructor(private valueService: ValueService) {
    this.alive = true;
    this.myValues = [];
  }

  ngAfterContentInit(): void {
    TimerObservable.create(0, 1000)
      .takeWhile(() => this.alive)
      .subscribe(() => {
        this.valueService.doSmth().subscribe(
          value => {
            this.myValues.push(value);
          }
        );
      });
  }
...

}
Run Code Online (Sandbox Code Playgroud)

父模板如下所示:

<ul>
  <li *ngFor="let value of myValues">
    <p>{{value.name}}</p>
  </li>
</ul>

<app-value-chart [chartData] = myValues></app-value-chart>
Run Code Online (Sandbox Code Playgroud)

这是我的孩子部分:

@Component({
  selector: 'app-value-chart',
  templateUrl: …
Run Code Online (Sandbox Code Playgroud)

angular

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

正确使用析构函数

我刚开始使用C++,现在我有一个非常基本的问题.

我写了两节课:

坐标:

#include <stdio.h>

class Coordinate {
private:
    int x;
    int y;
public:
    Coordinate(int a, int b) {
        x = a;
        y = b;
    };
    void printTest() {
        printf("%d %d\n", x, y);
    };
};
Run Code Online (Sandbox Code Playgroud)

测试:

class Test {
private:
    int q;
    Coordinate *point;
public:
    Test(int a, int b, int c) {
        q = a;
        point = new Coordinate(b, c);
    };
    virtual ~Test() {
        delete point;
    }
};
Run Code Online (Sandbox Code Playgroud)

主功能:

int main() {
    Test *test = new Test(1, 2, 3);
    // …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-memory-allocation

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

循环将数据追加到数据帧-函数仅返回数据帧的最后一行

我想创建一个循环访问大量文件的函数,计算每个文件的完整案例数,然后将新行附加到具有文件“ ID”编号及其对应完整数量的现有数据帧中案件。

在下面,我创建了一个仅返回数据帧最后一行的代码。我相信我的函数只会返回最后一行,因为R在每个循环中都会覆盖我的数据帧,但是我不确定。我在网上做了很多研究如何解决这个问题,但是我找不到一个简单的解决方案(我对R非常陌生)。

在下面,您可以看到我的代码和得到的输出:

complete <- function(directory = "specdata", id = 1:332) {
  files_list <- list.files("specdata", full.names = T) # creates a list of files

  dat <- data.frame() # creates an emmpty data frame

    for (i in id) {

    data <- read.csv(files_list[i]) # reads the file "i" in the id vector 

    nobs <- sum(complete.cases(data)) # counts the number of complete cases in that file  

    data_frame <- data.frame("ID" = i, nobs) # here I want to store the number of complete …
Run Code Online (Sandbox Code Playgroud)

r

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

正确使用移动语义

由于性能原因,我想在代码的这一部分中使用移动语义:

resultVector.push_back(GetEntry<T>(m, columnIndex, &readData));
delete[] readData.data;
Run Code Online (Sandbox Code Playgroud)

它看起来像这样:

resultVector.push_back(std::move(GetEntry<T>(m, columnIndex, &readData)));
delete[] readData.data;
Run Code Online (Sandbox Code Playgroud)

但是我不确定是否会导致delete[] readData.data;事后的不确定行为。

这是GetEntry函数:

template<typename T>
T GetEntry(int line, int col, hdfData<T> *hdfData) {
    int n_max = hdfData->dims[1];
    return hdfData->data[n_max * line + col];
}
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×2

angular ×1

arrays ×1

c ×1

dynamic-memory-allocation ×1

r ×1

syntax ×1