标签: initialization

用英文字母初始化矢量

我们可以有一个大小为26的字母向量,用英文字母初始化吗?我们可以立刻初始化这个矢量吗?

c++ initialization vector

-1
推荐指数
1
解决办法
1007
查看次数

c ++中的向量初始化

有人可以向我解释为什么Graph构造函数中的verticies.size()等于'4'但是在addEdge中再次调用verticies.size()等于0?程序在verticies [fromVertex] .addEdge(toVertex)崩溃,因为verticies的大小为0.

我确信这是我应该知道的,但我不会在哪里出错.

class Vertex {

public:
    int value;
    vector<int> adj;
    bool isVisited = false;

    Vertex(int _value)
    {
        value = _value;
    }

    void addEdge(int destination)
    {
        adj.push_back(destination);
    }   
};

class Graph
{

public:

    int vertexCount;    // No. of vertices            
    vector<Vertex> verticies;

    Graph(int _vertexCount)
    {
        this->vertexCount = _vertexCount;
        vector<Vertex> verticies;
        for (size_t i = 0; i < _vertexCount; i++)
        {
            Vertex v = Vertex(i);
            verticies.push_back(v);
        }
        cout << "verticies count " << verticies.size() << endl;
    }
    void addEdge(int fromVertex, int …
Run Code Online (Sandbox Code Playgroud)

c++ initialization vector

-1
推荐指数
1
解决办法
67
查看次数

我们如何在C ++中初始化所有值为0的向量

在数组中,我们可以执行int arr[100]={0}此操作 ,这会将数组中的所有值初始化为0。我尝试使用vector like进行相同操作 vector <int> v(100)={0},但它给出了错误 error: expected ‘,’ or ‘;’ before ‘=’ token。另外,如果我们可以通过C ++的“ memset”功能执行此操作,请也告诉该解决方案。

c++ stl initialization vector memset

-1
推荐指数
1
解决办法
6055
查看次数

我从int x得到了一个非常奇怪的输出[3];

所以我在C++中键入下面的代码

#include <iostream>
using namespace std;
int main() {
    int x[3];
    cout << x[1] << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它打印出-272632344而不是0.任何理由?

c++ initialization

-1
推荐指数
1
解决办法
64
查看次数

Why am I getting segmentation fault while implementing linked list?

In this function, I get segmentation fault. I think it has something to do with memory allocation. What mistake am I making?

Now, if I initialize Node* a =NULL, i get my head pointer as NULL in the end.

struct Node {
    int data;
    struct Node* next;
    Node(int x) {
        data = x;
        next = NULL;
    }
};

Node* addTwoLists(Node* first, Node* second) {
    // Code here
    Node *a;
    Node *head = a;
    int bor = 0;
    while(first->next && second->next) …
Run Code Online (Sandbox Code Playgroud)

c++ struct initialization linked-list singly-linked-list

-1
推荐指数
1
解决办法
83
查看次数

在 Scala 中初始化“最终”变量

考虑以下 Java 类:

public class C {
  private int n;
  private final int x;
  private final int y;
  public C(int n) {
    this.n = n;
    if (n < 0) {
      x = -1;
      y = -1;
    } else {
      x = 1;
      y = 1;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

x,y依赖n,在设置之后就不能改变了。这如何在 Scala 中实现?Aprivate val x是最终的,但既然是最终的,val它就不能在“构造函数”中更改。Aprivate var x是私有的,但不是最终的,因此虽然它对用户不可见,但对于可能错误地修改它的程序员来说是可见的。

在单个“最终”变量的情况下,可以像在这个答案中那样做,但是如果有两个(或更多,就像我实际编码的那样),那么我不知道该怎么做。这是一种方法:

class C(n: Int) {
  private val x = if (n < 0) …
Run Code Online (Sandbox Code Playgroud)

scala final initialization

-1
推荐指数
1
解决办法
91
查看次数

使用初始化列表之前的 C++ 数据验证

我正在实现一个 Triangle 类,其属性是 Point 类的三个私有对象。到目前为止,我在 Triangle 构造函数中使用初始化列表来初始化这三个点。问题是我必须在创建它时确定它是否是一个有效的三角形(检查两条边的总和是否大于第三条边)。我如何验证这一点?注意:我可以将三角形构造函数更改为如下所示:

Triangle(x0, y0, x1, y1, x2, y2)
Run Code Online (Sandbox Code Playgroud)

但我真的很想知道如果它看起来像这样该怎么做:

Triangle(p1, p2, p3)
Run Code Online (Sandbox Code Playgroud)

我的代码看起来像这样

class Point {
private:
   double x; 
   double y;
public:
   Point():x(0.0), y(0.0) {};
   Point(double x0, double y0):x(x0), y(y0){};
   double distance(Point p) {//distance between two points};
   //getters
   //setters
 };

class Triangle {
private:
   Point v1;
   Point v2;
   Point v3;
public:
   Triangle(Point p1, Point p2, Point p3): 
                                         v1(p1.getX(), p1.getY()),
                                         v2(p2.getX(), p2.getY()),
                                         v3(p3.getX(), p3.getY())  {
       //Edit
       if v1.distance(v2) + v2.distance(v3) <= v1.distance(v3) {
            //This is not …
Run Code Online (Sandbox Code Playgroud)

c++ validation constructor initialization

-1
推荐指数
1
解决办法
351
查看次数

尽管有 if/else 语句(两种情况下都已初始化),但我遇到“变量可能尚未初始化”的问题

protected int getGewichtung(String company)
{
    int index;

    for (HashMap<String,String> i : top3Compland) 
    {
        String test = String.join(_name, i.keySet());
        System.out.println(test);

        if (test == company) 
        {
            index = top3Compland.indexOf(i);
            
        } 
        else 
        {
            index = -5; 
        }
    }
    
    return index;
}
Run Code Online (Sandbox Code Playgroud)

我已经用 print 语句检查了它是否完全进入了 for 循环。但是所有的键都被打印出来,所以它确实输入了它,并且也确实发生了到字符串的转换,因为我首先只将有效的字符串放入函数中,所以它也必须肯定输入 if 语句,但即使它不输入然后它会输入 else 语句并用一个数字初始化它,这样它就不会抱怨变量“index”可能没有被初始化。我在监视什么??感谢您的帮助!

更新版本:我改进了一些缺陷,但出现了同样的错误。但我无法理解索引未初始化的情况......

    protected int getGewichtung(String company)
{
    int index;


   
    if (top3Compland.size() > 0 && top3Compland != null) 
    {
        for (HashMap<String,String> i : top3Compland) 
        {
            String test = String.join(_name, i.keySet());
            System.out.println(test);

            if (test.equals(company)) 
            {
                index …
Run Code Online (Sandbox Code Playgroud)

java variables initialization

-1
推荐指数
1
解决办法
55
查看次数

C# 类属性初始化

在其他现代编程语言中,当父类具有多个构造函数时,可以初始化只读属性,而无需重复代码。但 C# 似乎有一个相当烦人的限制。例如我有以下 C# 类:

class MyClass
{
    string? str1 { get; init; }
    string? str2 { get; init; }

    public MyClass(string s1)
    {
       this.str1 = s1;
       this.str2 = null;
    }

    public MyClass(string s1, string s2)
    {
       this.str1 = s1;
       this.str2 = s2;
    }
}
Run Code Online (Sandbox Code Playgroud)

可以修改构造函数来部分解决问题,如以下代码所示:

class MyClass
{
    string? str1 { get; init; }
    string? str2 { get; init; }

    public MyClass(string s1): this(s1, null) { }

    public MyClass(string s1, string s2)
    {
       this.str1 = s1;
       this.str2 = s2; …
Run Code Online (Sandbox Code Playgroud)

.net c# initialization

-1
推荐指数
1
解决办法
83
查看次数

为什么这段代码在变量初始化方面是非法的?

import java.util.*;

class Example {

  public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    System.out.print("Input an integer : ");
    int num = input.nextInt();
    int y;
    if (num > 100) {
      y = 200;
    }
    if (num < 100) {
      y = 200;
    }
    if (num == 100) {
      y = 200;
    }
    System.out.println(y);
  }
}
Run Code Online (Sandbox Code Playgroud)

错误:

Example.java:19: error: variable y might not have been initialized
    System.out.println(y);

                   ^
Run Code Online (Sandbox Code Playgroud)

java variables initialization

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