小编Joh*_*ica的帖子

如何映射到结构方法?

我正在尝试使用结构方法作为地图目标。当我尝试仅通过“self.method”引用映射参数中的方法时,出现错误“方法,而不是字段”。

这是一些简单的代码来展示我想要做什么

impl Astruc {
    fn map_function(&self, index: usize) -> usize {
        2 * index
    }
    fn map_attempt(&self) {
        (0..10).map(self.map_function)  // this causes the error: method, not a field
    }
}
Run Code Online (Sandbox Code Playgroud)

如何正确参考map_function

rust

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

git 是否存储与每次提交相关的机器和文件夹结构的信息?

我想知道 git 是否为每个提交保留与每个提交/推送的原始计算机相关的信息。

我问过 ChatGPT,它说 git 不存储这样的东西。但我想听听第二个意见。

git

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

使用lazy_static!符号表中定义变量的大小为0

以下 Rust 代码,在 x86_64 平台编译。

#[macro_use]
extern crate lazy_static;

use std::collections::HashMap;

lazy_static! {
    static ref HASHMAP: HashMap<u32, &'static str> = {
        let mut m = HashMap::new();
        m.insert(0, "foo");
        m.insert(1, "bar");
        m.insert(2, "baz");
        m
    };
}

fn main() {
    // First access to `HASHMAP` initializes it
    println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap());

    // Any further access to `HASHMAP` just returns the computed value
    println!("The entry for `1` is \"{}\".", HASHMAP.get(&1).unwrap());
}
Run Code Online (Sandbox Code Playgroud)

我使用readelf命令查看符号表中HASHMAP变量的大小:

readelf -sW target/debug/deps/section_test-4d7d6a03c56fdde3.o

Symbol table '.symtab' contains 590 …
Run Code Online (Sandbox Code Playgroud)

symbols elf segment rust lazy-static

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

找到后stl容器端是多余的,那么快捷方式是什么

让我们假设我们有一个地图类(无序地图,列表,集合,也可以做任何事情).我们正在寻找一个特定的元素.在调用find()成员之后,我们必须检查end()成员.但是find()在内部已经知道它是返回一个好的迭代器还是结束迭代器.我们为什么还需要再次调用end()?这增加了一些开销.

std::map<int,float> myMap;
// some other code to populate the map
// ...
std::map<int,float>::iterator myIt;
myIt = myMap.find(2); // doesn't this already know wheter its returning the end() iterator?
if (myIt != myMap.end()) { //calling end() here wastes some time because find
                           //already knew the result
   std::cout << "Found, value is "<<(*myIt).second<<"\n";
} else {
   std::cout << "Not found.\n";
}
Run Code Online (Sandbox Code Playgroud)

应该有一种方法可以在不调用end()的情况下知道find()的结果.

c++ stl

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

数组:java.lang.NullPointerException

我做错了什么?我认为这是因为阵列.行号现在不正确,因为我不得不将文字缩短一点,对不起.

Exception in thread "main" java.lang.NullPointerException
    at Team.<init>(Team.java:43)
    at Team.main(Team.java:30)
Run Code Online (Sandbox Code Playgroud)

码:

public static void main(String[] args) 
{
    System.out.println("Dein Fußballteam in der ersten Bundesliga!");

    int[] Spieler = new int[20];
    int Torschützen[] = new int[10];  

    Team myTeam = new Team();
    myTeam.einfacheNachricht();
}

public Team() 
{
    TeamSpieler = new Spieler[20];
    Scanner team = new Scanner(System.in);
    for (int i=0; i<20;i++){
        System.out.println("Bitte geben Sie den Namen des Spielers " + i + " ein: ");
        TeamSpieler[i].Name = team.nextLine();
        System.out.println("Bitte geben Sie das Alter des Spielers …
Run Code Online (Sandbox Code Playgroud)

java arrays nullpointerexception

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

"using namespace std;" 不工作

所以我昨晚开始学习C++,因为我想要进入一段时间,从我看过的几个教程,看起来可能有点简单,因为我认为我很好用PHP并且有经验是JAVA.

一个问题,编写我的第一个程序,以及我的"using namespace std;" 声明不会工作,当我构建程序即时抛出此错误:

C:\\Users\\p6735a\\Desktop\\Project\\game.cpp: In function `int main(int, char *)':
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: `string' undeclared (first use this         function)
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: (Each undeclared identifier is reported only once
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: for each function it appears in.)
C:\\Users\\p6735a\\Desktop\\Project\\game.cpp:6: parse error before `;'
[Finished in 0.1s with exit code 1]
Run Code Online (Sandbox Code Playgroud)

无论如何这里是代码:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    string input;
    int hp;

    cout << "You see a man. Would you like to kill him?\n1. Yes\n2. No" << endl;
    //cin >> …
Run Code Online (Sandbox Code Playgroud)

c++

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

malloc中number对象的用法

我是编程的新手,我正在努力理解它们之间的区别

A = (char * ) malloc(sizeof(char)*n);
Run Code Online (Sandbox Code Playgroud)

A = (char * ) malloc(sizeof(char)); 
Run Code Online (Sandbox Code Playgroud)

要么

A = new char [n];
Run Code Online (Sandbox Code Playgroud)

A = new char;
Run Code Online (Sandbox Code Playgroud)

当我没有指定特定数据类型的对象数时,编译器分配给此指针的默认内存是多少.

当我宣布时

A = new char [n];
cout << A[n+1];
Run Code Online (Sandbox Code Playgroud)

它没有给我一个分段错误.

它应该不会给出分段错误,因为我试图访问超出为阵列分配的内存.

c c++ malloc memory-management new-operator

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

无尽的循环:为什么这个无穷无尽

好的,我在这里遇到了一个小问题.不要介意他们为我提供的代码中的注释.

我的问题在于功能.我希望它进行测试,以确保再次[0] = y或n.如果它不循环,直到我输入正确的数字.

它现在做了什么:它无休止地循环,无论我投入什么.

我确实错过了一些东西吗?

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

//functions called
float wages_loop();

//start main 
int main(void)
{
    char status[10], another[10];
    char buffer[80];

    float wages, other_income, interest, dividends, test;
    int dependents;
    int single_acc = 0, mj_acc = 0, ms_acc = 0, sh_acc = 0;

    printf("Would you like to start: ");
    gets_s(another);

    while (another[0] = 'y')
    {
        //printf("What is your Status: ");
        //gets_s(status);


        wages = wages_loop();



        //printf("\n How much in Other Income. ");
        //gets_s(buffer);
        //other_income = …
Run Code Online (Sandbox Code Playgroud)

c

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

列出最后一行不包含模式的文件

我的文件的最后一行应该是“#”

如果我的 tail -n 1 * | grep -L "#"结果(standard input)显然是因为它正在通过管道传输。

希望有一个 grep 解决方案,而不是读取整个文件并只搜索最后一行。

unix bash grep

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

将两个列表的所有组合相乘

我真的想知道如何从两个列表中提取所有元素并相互相乘.例如,如果有两个列表

A=[1,3,5,7,9]
B=[2,4,6,8]
Run Code Online (Sandbox Code Playgroud)

我想做1X2,1X4,1X6,1x8,3x2 ......等.来自AX的一个元素来自B的一个元素.我试图使用zip但由于长度差异,我无法得到正确的答案.

python list

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