小编tez*_*tez的帖子

如何将格式化的字符串写入文件?

我想将函数的输出写入文件.我预计这write_fmt就是我的要求:

use std::{
    fs::File,
    io::{BufWriter, Write},
};

fn main() {
    let write_file = File::create("/tmp/output").unwrap();
    let mut writer = BufWriter::new(&write_file);

    // From my function
    let num = 1;
    let factorial = 1;

    writer.write_fmt("Factorial of {} = {}", num, factorial);
}
Run Code Online (Sandbox Code Playgroud)

错误

error[E0061]: this function takes 1 parameter but 3 parameters were supplied
  --> src/main.rs:11:12
   |
11 |     writer.write_fmt("Factorial of {} = {}", num, factorial);
   |            ^^^^^^^^^ expected 1 parameter
Run Code Online (Sandbox Code Playgroud)

这似乎是错误的,文档中没有太多可用的内容.

rust

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

什么时候复制构造函数被调用

例如,我在对象初始化期间研究过

string s = "Hello world";
Run Code Online (Sandbox Code Playgroud)

如果RHS可隐式转换为LHS类型对象,则将调用Copy Constructor.但我有一个朋友非常肯定char会将指针作为参数的构造函数char被调用.但我告诉他只有在下面的情况下才会调用带指针的构造函数

string s("Hello world");
Run Code Online (Sandbox Code Playgroud)

那是对的吗?

c++

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

JSON 打印从根到叶的所有路径

[
        {
            "name": "Basic",
            "id": "home",
            "childrens": [
                {
                    "name": "Dashboard",
                    "viewtype": "custom",
                    "view": "dashboard.html",
                    "childrens": []
                },
                {
                    "name": "DeviceInfo",
                    "href": "WSettings",
                    "childrens": [
                        {
                            "name": "DeviceInfo Form",
                            "childrens": [
                                {
                                    "name": "DeviceInfo Form1",
                                    "viewtype": "xml",
                                    "view": "dinfo",
                                    "childrens": []
                                },
                                {
                                    "name": "DeviceInfo Form2",
                                    "viewtype": "xml",
                                    "view": "complexjson",
                                    "childrens": []
                                }
                            ]
                        },
                        {
                            "name": "DeviceInfo Table",
                            "childrens": [
                                {
                                    "name": "DeviceInfo Table1",
                                    "viewtype": "xml",
                                    "view": "dinfotable",
                                    "childrens": []
                                },
                                {
                                    "name": "DeviceInfo Table2",
                                    "viewtype": "xml",
                                    "view": "jsontable", …
Run Code Online (Sandbox Code Playgroud)

python json

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

如何在XSD中使用xsi:type限制XML元素的值?

我想基于属性值验证元素的文本值.例如

<Device xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Parameter xsi:type="xsd:unsignedInt">-100</Parameter>
  <Parameter xsi:type="xsd:boolean"></Parameter>
  <Parameter>hello</Parameter>
</Device>
Run Code Online (Sandbox Code Playgroud)

以上都应该失败.对于布尔值,除"true"或"false"(甚至空字符串)外,不应接受任何内容

我的XML是复杂得多用很多ObjectParameter节点,这是我的xsd这是递归地验证所有的节点

    <xs:complexType name="deviceType">
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="Object"/>
                    <xs:element ref="Parameter"/>
            </xs:choice>
    </xs:complexType>

    <xs:complexType name="objType">
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="Object"/>
                    <xs:element ref="Parameter"/>
            </xs:choice>
            <!-- Add all valid attributes for 'Object' type here -->
            <xs:attribute name="Id" use="required"/>
            <xs:attribute name="Flag" use="required"/>
            <xs:anyAttribute processContents="lax"/>
    </xs:complexType>

    <xs:complexType name="paramType" mixed="true">
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="Object"/>
                    <xs:element ref="Parameter"/>
            </xs:choice>
            <xs:attribute name="Id" use="required"/>
            <xs:attribute name="Flag" use="required"/>
            <xs:anyAttribute processContents="lax"/>
    </xs:complexType>
Run Code Online (Sandbox Code Playgroud)

但是我面临这个错误.

Type 'xsd:unsignedInt' is …
Run Code Online (Sandbox Code Playgroud)

xml xsd

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

使用字符指针测试条件

int main()
{
  char *p,c;
  for(p="Hello World";c=*p;++p)
  {
    printf("%c",c);
  }
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我知道++ p会使指针'p'指向"Hello World"中的下一个字符.而且我也知道在C或C++中没有对数组执行边界检查.程序是'Hello World'.我如何使用测试条件

c=*p;
Run Code Online (Sandbox Code Playgroud)

什么'c =*p'返回.据我所知,当'++ p'到达'hello world'的末尾时,指针'p'应该指向一些垃圾值并且循环应该打印一些垃圾值.

c c++

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

使用Arduino作为常规AVR

我打算用ATmega做一个项目(由于我大学的限制,我不能直接使用Arduino).但我真的想使用Arduino的IDE,串行监视器,使用Processing绘制图形以进行调试.我可以将常规的ATmega代码转储到Arduino中并使用串行监视器进行调试吗?

我可以像任何其他正常的AVR开发板一样使用Arduino Uno板,这样我就可以充分利用这两个世界.我用Google搜索了,但我得不到我需要的答案.

avr arduino

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

线程同步行为

class Untitled {
    public static void main(String[] args) {
        MyRunnable r1 = new MyRunnable();
        Thread t1 = new Thread(r1,"Thread 1:");
        Thread t2 = new Thread(r1,"Thread 2:");
        t1.start();
        t2.start();

    }
}

class MyRunnable implements Runnable
{
    String s1 = "Hello World";
    String s2 = "Hello New World";
    public void run()
    {
        synchronized(s1)
        {
            for(int i =0;i<3;++i)
            System.out.println(Thread.currentThread().getName()+s1);

        }
        synchronized(s2)
        {
            for(int i = 0;i<3;++i)
            System.out.println(Thread.currentThread().getName()+s2);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

Thread 1:Hello World
Thread 1:Hello World
Thread 1:Hello World
Thread 1:Hello New …
Run Code Online (Sandbox Code Playgroud)

java multithreading synchronized

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

为什么println打印引用对象的值而不是对象的地址

String myString="Plz_help";
Run Code Online (Sandbox Code Playgroud)

我开始知道'myString'不是一个对象,而是一个对象引用变量,它存储了实际对象的基地址.所以'myString'应该在我执行时返回一个地址

System.out.println(myString);
Run Code Online (Sandbox Code Playgroud)

但是它将'Plz_help'返回到输出.

Myclass obj_ref_var=new Myclass();
Run Code Online (Sandbox Code Playgroud)

当我为我创建的其他类使用相同的System.out.println(obj_ref_var)时,它返回地址.

java reference

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

源文件和头文件

我在理解源文件和头文件方面遇到了困难.假设

1)我有一个源文件(functions.cpp),其中包含位置中名为'int add(int x,int y)'的函数/Users/xyz/Desktop/functions.cpp.

2)functions.h包含源文件(functions.cpp)中函数声明的头文件()放在/Users/xyz/Documents/function.h

3)main.cpp包含'main()'函数的其他源文件()需要调用'functions.cpp'中定义的'add()'函数.源文件'main.cpp'位于/Users/xyz/Downloads/main.cpp

我将这些文件放在不同的位置,以便我能更好地理解这些概念.

那么,我如何链接function.cppmain.cpp使用functions.h.

   #include "   "
Run Code Online (Sandbox Code Playgroud)

我应该在上面使用的路径是什么include

另外,我的理解是.h文件提供了函数的声明,这些函数在其他地方被定义,并且编译器需要声明来调用在一些其他未定义的文件或函数中定义的函数.是对的吗?如果我错了,请纠正我.

c c++

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

全局变量初始化

原则上,在调用main()之前初始化在任何函数外部定义的变量(即全局,名称空间和类静态变量).翻译单元中的这些非局部变量按其声明顺序初始化

以上是我的讲师给出的课堂笔记.

#include <iostream>

using namespace std;
int a=99;
int b;
int main(int argc, char *argv[]) 
{
  cout<<a<<endl;
  cout<<b<<endl;
  return 0;
}
b=100;
Run Code Online (Sandbox Code Playgroud)

我运行它时出错.在调用main()之前'b'分配给100是不是真的?错误是 C++ requires a type specifier for all declarations

c++

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

线程创建和启动

两者之间有区别吗?

class MyThread extends Thread
{
}

MyThread obj = new MyThread();
Thread t = new Thread(obj);
t.start()
Run Code Online (Sandbox Code Playgroud)

VS

obj.start()
Run Code Online (Sandbox Code Playgroud)

选择一个优于其他优势是否有任何优势?

java

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

Java字段初始化

假设我B有一个类C类型成员作为字段的类.

现在我编写了一个构造函数,B以便它的默认构造函数不会被合成.

B's构造函数不会C显式初始化该类型字段.

Class B
{
  C obj_c;

  B()
  { }
}
Run Code Online (Sandbox Code Playgroud)

在C++默认构造函数obj_c中将被称为coz默认构造函数的非初始化成员将被隐式调用为Object类型成员.关于Java的问题?

如果构造函数没有在Java中初始化字段会发生什么?

java

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

列表的迭代器类型

我知道vector<double>::iterator返回随机访问迭代器类型.返回的迭代器类型是什么list<double>::iterator.它是双向迭代器吗?

c++

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

标签 统计

c++ ×5

java ×4

c ×2

arduino ×1

avr ×1

json ×1

multithreading ×1

python ×1

reference ×1

rust ×1

synchronized ×1

xml ×1

xsd ×1