我很好奇,首先,为什么std::list并将std::forward_list排序函数作为成员函数包含在内,与其他标准库容器不同.但对我来说更有趣的是,CPPReference和CPlusPlus都声称这种排序是在O(n log n)时间内完成的.
我甚至无法想象如何在没有随机访问元素的情况下对容器进行排序.所以我把测试组合起来,forward_list尽可能地使它变得困难.
#include <chrono>
#include <cstdint>
#include <deque>
#include <forward_list>
#include <iostream>
#include <random>
using std::endl;
using namespace std::chrono;
typedef nanoseconds::rep length_of_time;
constexpr int TEST_SIZE = 25000;
class Stopwatch
{
public:
void start_timing();
void end_timing();
length_of_time get_elapsed_time() const;
private:
time_point<high_resolution_clock> start;
time_point<high_resolution_clock> end;
length_of_time elapsed_time = 0;
};
void Stopwatch::start_timing()
{
start = high_resolution_clock::now();
}
void Stopwatch::end_timing()
{
end = high_resolution_clock::now();
auto elapsed = end - …Run Code Online (Sandbox Code Playgroud) 我有一个大数字的标准库容器,如果我将它们加在一起它们可能会导致溢出.让我们假装它是这个容器:
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Run Code Online (Sandbox Code Playgroud)
我想用std :: accumulate来计算这个容器的平均值,但我不能将所有数字加在一起.我只是计算它v[0]/v.size() + v[1]/v.size() + ....所以我设置:
auto lambda = ...;
std::cout << std::accumulate(v.begin(), v.end(), 0, lambda) << std::endl;
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所尝试的,其中->表示输出:
lambda = [&](int a, int b){return (a + b)/v.size();}; -> 1
lambda = [&](int a, int b){return a/v.size() + b/v.size();}; -> 1
lambda = [&](int a, int b){return a/v.size() + b;}; -> 10
Run Code Online (Sandbox Code Playgroud)
我怎样才能产生正确的平均值,以便输出5?
如果你想拆分以空格分隔的单词列表,你可以使用
def words(text)
return text.split.map{|word| word.downcase}
end
Run Code Online (Sandbox Code Playgroud)
类似于Python的列表理解:
words("get out of here")
Run Code Online (Sandbox Code Playgroud)
返回["get", "out", "of", "here"].如何将块应用于字符串中的每个字符?
该文档的runtime.LockOsThread状态:
LockOSThread将调用goroutine连接到其当前操作系统线程.在调用goroutine退出或调用UnlockOSThread之前,它将始终在该线程中执行,而没有其他goroutine可以执行.
但考虑一下这个程序:
package main
import (
"fmt"
"runtime"
"time"
)
func main() {
runtime.GOMAXPROCS(1)
runtime.LockOSThread()
go fmt.Println("This shouldn't run")
time.Sleep(1 * time.Second)
}
Run Code Online (Sandbox Code Playgroud)
该main够程被连接到一个可用的操作系统线程的设置GOMAXPROCS,所以我预计在3线创建的够程main将无法运行.但相反,程序打印This shouldn't run,暂停1秒,然后退出.为什么会这样?
这是一个简短的例子来证明:
package main
import "fmt"
func main() {
array := [3]int{1, 2, 3}
array[0]++ // Works
slice := make([]int, 3)
for i := range slice {
slice[i] = i + 1
}
arrayMap := make(map[int][3]int)
sliceMap := make(map[int][]int)
arrayMap[0] = array
sliceMap[0] = slice
//arrayMap[0][0]++ // Does not compile: "cannot assign to arrayMap[0][0]"
sliceMap[0][0]++
fmt.Println(arrayMap)
fmt.Println(sliceMap)
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能修改数组的内容,如果它在地图中,即使它们在地图外是可变的?为什么这适用于切片?
我发现了以下特点:
b := "a"[0]
r := 'a'
fmt.Println(b == r) // Does not compile, cannot compare byte and rune
fmt.Println("a"[0] == 'a') // Compiles and prints "true"
Run Code Online (Sandbox Code Playgroud)
这是如何运作的?
我有一个std::string。我想要一组唯一的字符,每个字符都用表示std::string。
我可以轻松获得一组字符:
std::string some_string = ...
std::set<char> char_set(some_string.begin(), some_string.end());
Run Code Online (Sandbox Code Playgroud)
我可以将它们转换为这样的字符串:
std::set<std::string> string_set;
for (char c: char_set) {
string_set.emplace(1, c);
}
Run Code Online (Sandbox Code Playgroud)
但是这种方法似乎很尴尬。是否有更好的方法(最好是标准库单行)?
当我尝试编译使用 boost 和 asio 的 cmake 项目时,make出现以下错误:
CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `__cxx_global_var_init1':
/usr/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()' CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `__cxx_global_var_init1':
/usr/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `__cxx_global_var_init2':
/usr/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `__cxx_global_var_init3':
/usr/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
CMakeFiles/client-network-handler-test.dir/main.cpp.o: In function `boost::asio::error::get_system_category()':
/usr/include/boost/asio/error.hpp:216: undefined reference to `boost::system::system_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `__cxx_global_var_init1':
/usr/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `__cxx_global_var_init2':
/usr/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
../../lib/libclient-network-handler.a(ClientNetworkHandler.cpp.o): In function `__cxx_global_var_init3':
/usr/include/boost/system/error_code.hpp:224: undefined reference to …Run Code Online (Sandbox Code Playgroud) 如果引导语言中的编译器运行良好且可维护,为什么要更改它?举例来说,重新编写它的编译器在版本1.5中自我托管,这导致编译时间变慢:当Go的目标是快速编译时,这显然是有害的.
我正在编写一个封装二维数组的类.这是复制构造函数.(WIDTH并且HEIGHT是编译时常量,这就是我认为它适合使用数组的原因.)
MyClass::MyClass(const MyClass &other)
{
std::copy(
&array[0][0], &array[0][0] + WIDTH*HEIGHT,
&other.array[0][0]);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用一个根据这个问题正确的方法,并且在我将原型更改为const &简单的pass-by-value之前工作.但是,我现在收到此编译器错误:
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
from /usr/include/c++/4.8/string:40,
from MyClass.hpp:4,
from MyClass.cpp:1:
/usr/include/c++/4.8/bits/stl_algobase.h: In instantiation of ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = ArrayDataType*; _OI = const ArrayDataType*]’:
/usr/include/c++/4.8/bits/stl_algobase.h:428:38: required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = ArrayDataType*; _OI = const ArrayDataType*]’
/usr/include/c++/4.8/bits/stl_algobase.h:460:17: required from ‘_OI std::copy(_II, _II, _OI) [with …Run Code Online (Sandbox Code Playgroud) 我正在尝试运行CPPReference的regex_search示例:
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::string lines[] = {"Roses are #ff0000",
"violets are #0000ff",
"all of my base are belong to you"};
std::regex color_regex("#([a-f0-9]{2})"
"([a-f0-9]{2})"
"([a-f0-9]{2})");
for (const auto &line : lines) {
std::cout << line << ": "
<< std::regex_search(line, color_regex) << '\n';
}
std::smatch color_match;
for (const auto &line : lines) {
std::regex_search(line, color_match, color_regex);
std::cout << "matches for '" << line << "'\n";
for (size_t i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我能得到的最接近的是runtime.GOARCH,但这也可能给出arm,它可以是 32 位或 64 位。
我只关心这个程序是如何构建的,而不关心操作系统是否也支持 64 位可执行文件。例如,对于 AArch64 CPU 上的 ARM 模式或 x86-64 CPU 上的 32 位兼容模式,我仍然想要 32,因为这是该程序运行的模式。
相关:检测操作系统 x86 或 x64,当在 GO 中编译为 x86 时,是关于检测操作系统支持的内容,例如可能运行不同编译的可执行文件。
c++ ×6
go ×4
arrays ×2
32bit-64bit ×1
accumulate ×1
boost ×1
boost-asio ×1
byte ×1
clang++ ×1
cmake ×1
dictionary ×1
lambda ×1
linked-list ×1
regex ×1
ruby ×1
rune ×1
sorting ×1
stdset ×1
string ×1
string-split ×1