我刚才因为这个问题而学到了这个,标准是std::complex(26.4 [complex.numbers]):
4如果
z是cv 类型的左值表达式,std::complex<T>则:
- 表达式reinterpret_cast<cv T(&)[2]>(z)应格式良好,
-reinterpret_cast<cv T(&)[2]>(z)[0]应指定的实部z,并且
-reinterpret_cast<cv T(&)[2]>(z)[1]应指定虚部z.
此外,如果a是类型为cv 的表达式,std::complex<T>*并且表达式a[i]是为整数表达式定义的i,则:
-reinterpret_cast<cv T*>(a)[2*i]应指定的实部a[i],并且
-reinterpret_cast<cv T*>(a)[2*i + 1]应指定虚部a[i].
这是我真正想要以符合标准的方式利用的东西.有些时候我有POD,比如数学向量,它们由单一数据类型组成.这是两个示例类:
template <typename T, unsigned N>
struct Vector
{
T v[N];
};
template <typename T>
struct Quaternion
{
T r, i, …Run Code Online (Sandbox Code Playgroud) 为了一点乐趣,我想在Rust中创建一个简单的HTTP请求.我把它扔在一起,效果很好:
use std::io::TcpStream;
fn main() {
// This just does a "GET /" to www.stroustrup.com
println!("Establishing connection...");
let mut stream = TcpStream::connect("www.stroustrup.com:80").unwrap();
println!("Writing HTTP request...");
// unwrap() the result to make sure it succeeded, at least
let _ = stream.write(b"GET / HTTP/1.1\r\n\
Host: www.stroustrup.com\r\n\
Accept: */*\r\n\
Connection: close\r\n\r\n").unwrap();
println!("Reading response...");
let response = stream.read_to_string().unwrap();
println!("Printing response:");
println!("{}", response);
}
Run Code Online (Sandbox Code Playgroud)
回应是:
Establishing connection...
Writing HTTP request...
Reading response...
Printing response:
HTTP/1.1 200 OK
...and the rest of the long HTTP …Run Code Online (Sandbox Code Playgroud) 有时候我想从std::io::Reader 读取一个字节.如果我尝试这样做:
use std::io::{self, Read};
fn main() {
let mut byte: u8 = 0;
io::stdin().read(&mut byte).unwrap();
println!("byte: {}", byte);
}
Run Code Online (Sandbox Code Playgroud)
我得到以下错误(这是明确的,因为byte不是切片):
error[E0308]: mismatched types
--> src/main.rs:6:22
|
6 | io::stdin().read(&mut byte).unwrap();
| ^^^^^^^^^ expected slice, found u8
|
= note: expected type `&mut [u8]`
found type `&mut u8`
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以保持byte简单u8,只需要切片,然后我可以传递给它read()?使这段代码工作的显而易见的方法是使用长度为1的数组:
use std::io::{self, Read};
fn main() {
let mut byte: [u8; 1] = [0];
io::stdin().read(&mut byte).unwrap();
println!("byte: {}", byte[0]);
}
Run Code Online (Sandbox Code Playgroud)
但是在整个代码中,这有点奇怪的感觉,使用单个u8 …
我正在设计一个UI,我有一个情况,我有一个QLineEdit,我只需要接受整数.
我知道如何设置验证器在代码中使用QIntValidator.但我想在设计师中这样做.
如何在使用设计器时设置QLineEdit的验证器?
注意:我不想使用输入掩码.输入掩码和验证器功能不同.
该
vfork()函数具有相同的效果fork(2),除非在成功调用[...]exec(3)函数族之一之前,如果进程调用任何其他函数,则行为未定义.
这表明exec*()之后调用任何函数vfork()是可以接受的.但是,稍后在手册页中明确说明:
特别是,程序员不能依赖父母保持阻止,直到孩子打电话
execve(2)[...].
execve(2)在手册页中重复使用,它的用法表明它是唯一exec可以接受的类型函数vfork().
那么为什么execve被挑选出来,我可以安全地调用其他exec类型的函数(比如execlp)?
受我在上一个问题中观察的启发,我决定做一点测试:
#include <iostream>
#include <sstream>
int main()
{
char c = 'A';
std::stringstream ss("B");
// I know this is bad mojo; that's why I'm testing it
ss >> char(c);
std::cout << c << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我的编译器版本:
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
在C++ 03模式下编译clang,它编译并运行正常:
$ clang++ -Wall -pedantic -std=c++03 test.cpp
test.cpp:9:6: warning: expression result unused [-Wunused-value]
ss >> char(c);
~~ ^ ~~~~~~~
1 warning generated.
$ ./a.out
A
Run Code Online (Sandbox Code Playgroud)
它打印出来A,这很好,因为这个代码甚至不应该编译.切换到C++ …
我正在写一些处理一些文件的bash/zsh脚本.我想为某个类型的每个文件执行一个命令,其中一些命令重叠.当我尝试时find -name 'pattern1' -or -name 'pattern2',只使用最后一个模式(pattern1不返回匹配的文件;只匹配文件pattern2).我想要的是匹配pattern1或pattern2匹配的文件.
例如,当我尝试以下操作时,这就是我得到的(仅./foo.xml发现并打印通知):
$ ls -a
. .. bar.html foo.xml
$ tree .
.
??? bar.html
??? foo.xml
0 directories, 2 files
$ find . -name '*.html' -or -name '*.xml' -exec echo {} \;
./foo.xml
$ type find
find is an alias for noglob find
find is /usr/bin/find
Run Code Online (Sandbox Code Playgroud)
使用-o而不是-or给出相同的结果.如果我切换-name参数的顺序,那么只bar.html返回而不是foo.xml.

为什么不bar.html和foo.xml找到并归还?我如何匹配多个模式?
参数可以传递给函数并修改:
fn set_42(int: &mut i32) {
*int += 42;
}
fn main() {
let mut int = 0;
set_42(&mut int);
println!("{:?}", int);
}
Run Code Online (Sandbox Code Playgroud)
输出:
42
Run Code Online (Sandbox Code Playgroud)
天真地改变代码以使用切片失败了一大堆错误:
fn pop_front(slice: &mut [i32]) {
*slice = &{slice}[1..];
}
fn main() {
let mut slice = &[0, 1, 2, 3][..];
pop_front(&mut slice);
println!("{:?}", slice);
}
Run Code Online (Sandbox Code Playgroud)
输出:
<anon>:2:14: 2:27 error: mismatched types:
expected `[i32]`,
found `&[i32]`
(expected slice,
found &-ptr) [E0308]
<anon>:2 *slice = &{slice}[1..];
^~~~~~~~~~~~~
<anon>:2:14: 2:27 help: see the detailed explanation …Run Code Online (Sandbox Code Playgroud) __attribute__ ((__packed__))嵌套结构有什么影响?例如:
// C version
struct __attribute__ ((__packed__))
{
struct
{
char c;
int i;
} bar;
char c;
int i;
} foo;
// C++ version
struct __attribute__ ((__packed__)) Foo
{
struct Bar
{
char c;
int i;
} bar;
char c;
int i;
} foo;
Run Code Online (Sandbox Code Playgroud)
我知道foo会紧紧包装,但是怎么样bar?它会紧紧包装吗?是否__attribute__ ((__packed__))使嵌套struct也包装好?
请考虑以下代码:
#include <vector>
#include <algorithm>
template <typename Input1, typename Input2, typename Output>
void merge(Input1 begin1, Input1 end1, Input2 begin2, Input2 end2, Output out)
{
}
int main()
{
std::vector<int> a = {1, 2};
int b[] = {3, 4};
int c[4];
merge(a.begin(), a.end(), b, b + 2, c);
}
Run Code Online (Sandbox Code Playgroud)
汇总收益率:
$ clang++ -std=c++11 -stdlib=libc++ merge.cpp
merge.cpp:15:5: error: call to 'merge' is ambiguous
merge(a.begin(), a.end(), b, b + 2, c);
^~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:4056:1: note:
candidate function [with _InputIterator1 = std::__1::__wrap_iter<int *>,
_InputIterator2 = …Run Code Online (Sandbox Code Playgroud)