我希望将字符串数组转换为GO中的字节数组,以便将其写入磁盘.将字符串数组([]string)编码和解码为字节数组([]byte)的最佳解决方案是什么?
我想要迭代字符串数组两次,第一次获得字节数组所需的实际大小,然后第二次[]byte(str)为每个元素写入长度和实际字符串().
解决方案必须能够以另一种方式转换它; 从a []byte到a []string.
我试图将我的项目拆分成多个文件,但是我将问题导入到我的问题中,main.rs因为它说Column的字段是私有的,但我已将结构声明为public.
SRC/column.rs
pub struct Column {
name: String,
vec: Vec<i32>,
}
Run Code Online (Sandbox Code Playgroud)
SRC/main.rs
pub mod column;
fn main() {
let col = column::Column{name:"a".to_string(), vec:vec![1;10]};
println!("Hello, world!");
}
Run Code Online (Sandbox Code Playgroud)
货物建造
src/main.rs:4:15: 4:75 error: field `name` of struct `column::Column` is private
src/main.rs:4 let col = column::Column{name:"a".to_string(), vec:vec![1;10]};
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:4:15: 4:75 error: field `vec` of struct `column::Column` is private
src/main.rs:4 let col = column::Column{name:"a".to_string(), vec:vec![1;10]};
Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的代码更新为ES6,因为我使用的是Node 4.0,并且到目前为止它非常喜欢它的功能.但是我遇到了新的ES6 Map数据结构的问题,因为它与用作密钥{}时的行为不同Array.我用它作为计数器图.
我运行此代码,我想知道如何使用数组作为键Map.
"use strict";
var a = new Map();
a.set(['x','y'], 1);
console.log(a.get(['x','y']));
var b = {};
b[['x','y']] = 1;
console.log(b[['x','y']]);
Run Code Online (Sandbox Code Playgroud)
它打印出以下内容,第一行应该是,1而不是undefined:
undefined
1
Run Code Online (Sandbox Code Playgroud)
原始的JS映射将字符串化,我不想与新的ES6进行相同类型的stringify hack Map.
如何将数组作为ES6的可靠键使用Map?
我正在查看代码,Vec<T>看看它iter()是如何实现的,因为我想为我的struct实现迭代器:
pub struct Column<T> {
name: String,
vec: Vec<T>,
...
}
Run Code Online (Sandbox Code Playgroud)
我的目标不是暴露字段并提供迭代器来为列执行循环,最大值,最小值,求和,平均等.
fn test() {
let col: Column<f32> = ...;
let max = col.iter().max();
}
Run Code Online (Sandbox Code Playgroud)
我想我会看到Vec<T>迭代是怎么回事.我可以看到iter()在定义SliceExt,但它的实现[T],而不是Vec<T>让我很为难,你怎么能叫iter()的Vec<T>?
我正在解析一个~500GB的日志文件,我的C++版本需要3.5分钟,我的Go版本需要1.2分钟.
我正在使用C++的流来传输文件的每一行以进行解析.
#include <fstream>
#include <string>
#include <iostream>
int main( int argc , char** argv ) {
int linecount = 0 ;
std::string line ;
std::ifstream infile( argv[ 1 ] ) ;
if ( infile ) {
while ( getline( infile , line ) ) {
linecount++ ;
}
std::cout << linecount << ": " << line << '\n' ;
}
infile.close( ) ;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
首先,为什么使用这段代码这么慢?其次,我如何改进它以使其更快?
我不知道如何将a Vec<i32>转换为&[u8]切片.
fn main() {
let v: Vec<i32> = vec![1; 100_000_000];
let v_bytes: &[u8] = /* ... */;
}
Run Code Online (Sandbox Code Playgroud)
我想写一个大Vec<i32>文件到一个文件,所以我可以在将来阅读它.
我有问题使我的通用InMemoryColumn<T>序列化.它抱怨'Encodable'和'Decodable'特性是私密的,但我认为它在这里是公开的.我如何实现这些特征,以便我可以编码和解码底层Vec<T>.
这是导入的代码:
extern crate bincode;
extern crate libc;
extern crate "rustc-serialize" as rustc_serialize;
use rustc_serialize::serialize::{Encodable,Decodable};
//import other libs
pub struct InMemoryColumn<T> {
name: String,
data: Vec<T>,
}
impl<T: Eq + Ord + Hash + Encodable + Decodable> InMemoryColumn<T> {
fn save(&self, tbl_name: &str) {
//encode self.data and write to disk
}
fn load(path: &str, name: &str) -> Result<InMemoryColumn<T>,String> {
//decode from disk and populate InMemoryColumn<T>
}
}
Run Code Online (Sandbox Code Playgroud) RocksDB 表示它可以存储任意数据,但 API 仅支持std::string类型。我想存储std::vector<T>值,如果我想这样做,那么我必须将它转换为std::string.
有没有一种不那么脆弱的方式来存储任意类型?
我想知道为什么D代码这么慢?我最初使用std.algorithm.sum,但性能更差.
我的D代码:
import std.algorithm;
import std.stdio;
void main()
{
immutable int n = 10000000;
int[] v = new int[n];
fill(v,1);
int total = 0;
foreach (int i; 0 .. n) {
total += v[i];
}
writeln(total);
}
Run Code Online (Sandbox Code Playgroud)
建造使用:
dmd -O arraysum.d
Run Code Online (Sandbox Code Playgroud)
等效C代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
const int n = 10000000;
int *v = malloc(n * sizeof(int));
for (int i = 0; i < n; ++i) {
v[i] = 1;
}
int total = 0;
for (int …Run Code Online (Sandbox Code Playgroud) 我正在尝试安装和使用DataStructures包,它似乎没有工作; 或者我错过了什么.
Pkg.init()
Pkg.status()
Pkg.add("DataStructures")
Pkg.status()
Pkg.update()
d = OrderedDict(Char,Int)
ERROR: OrderedDict not defined
Run Code Online (Sandbox Code Playgroud)
有什么问题?
我正在通过编写公共数据结构来学习c ++,并且我有一个编译器警告我没有定义我的内联添加方法.
src/vector.h:10:14: warning: inline function 'Vector::add' is not defined
[-Wundefined-inline]
inline void add(const float val);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?据我所知,方法匹配.但是,如果我删除内联方法,它工作正常,但第一次调用add需要11380us,但第二次和第三次是3667us左右 - 大约4倍的罚款成本.
SRC/vector.h
//#include <cstddef>
class Vector {
public:
explicit Vector(const int n);
explicit Vector(const int n, const float val);
float& operator[](const int i);
inline int const length();
inline void fill(const float val);
inline void add(const float val);
inline float sum();
private:
float* arr;
int len;
};
Run Code Online (Sandbox Code Playgroud)
src.vector.cpp
#include "vector.h"
#include <iostream>
#include <algorithm>
#include "yepCore.h"
#include "yepMath.h"
#include "yepLibrary.h"
#include <cstdlib>
using …Run Code Online (Sandbox Code Playgroud) rust ×4
c++ ×2
arrays ×1
c++11 ×1
d ×1
dictionary ×1
ecmascript-6 ×1
es6-map ×1
file-io ×1
go ×1
inline ×1
javascript ×1
julia ×1
performance ×1
rocksdb ×1