我正在尝试实现一个基本的shell.
我需要读取用户输入,直到按下某些分隔符,才能执行相应的操作.这些分隔符可以是单个"a",单个"b"或单个"c".
输入示例如下所示(>shell提示符在哪里):
> 111-222-333-444a
Ok, '111-222-333-444' entered
Run Code Online (Sandbox Code Playgroud)
因为我想听一下键盘事件如'向上箭头'来删除当前命令并打印最后一个命令(实现历史记录功能).
因为我想听'tabulation'这样的键盘事件来自动完成当前命令(实现自动完成功能).
到目前为止,我的代码如下所示:
bool done = false;
char c;
while (!done && std::cin.get(c))
{
switch (c)
{
case 'a':
// Do something corresponding to 'a'
done = true;
break;
case 'b':
// Do something corresponding to 'b'
done = true;
break;
case 'c':
// Do something corresponding to 'c'
done = true;
break;
default:
// buffer input until a delimiter is pressed
break;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,循环似乎只有在按下"新行"键后才会执行.此行为会终止用户输入的交互式本质. …
我在Boost.Asio文档中尝试了不同的教程,并试图用C++ 11替换boost组件.但是,我在Timer.5中使用std :: bind时出错了 - 在多线程程序中同步处理程序.这是建议的代码:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer { /* Not relevent here */ };
int main()
{
boost::asio::io_service io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图替换boost::threadby std::thread和boost::bindby std::bind.这是我的代码:
#include <functional>
#include <iostream>
#include <thread>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer { /* Not relevent here */ };
int main() {
boost::asio::io_service io;
printer p(io);
std::thread …Run Code Online (Sandbox Code Playgroud) 是否std::async适用于模板功能?我试图在std::reverse异步任务中遇到编译时错误.
我试图使用更简单的函数(foo和bar)并发现只有非模板函数才有效.
#include <algorithm>
#include <future>
#include <string>
void foo(std::string::iterator first, std::string::iterator last)
{
}
template<class BidirectionalIterator>
void bar(BidirectionalIterator first, BidirectionalIterator last)
{
}
int main()
{
std::string str = "Lorem ipsum, dolor sit amet";
auto result_reverse = std::async(std::reverse, str.begin(), str.end()); // Compile-time error
auto result_foo = std::async(foo, str.begin(), str.end());
auto result_bar = std::async(bar, str.begin(), str.end()); // Compile-time error
result_reverse.get();
result_foo.get();
result_bar.get();
}
Run Code Online (Sandbox Code Playgroud)
编译器错误如下:
main.cpp: In function ‘int main()’:
main.cpp:18:71: erreur: no matching function for call …Run Code Online (Sandbox Code Playgroud) 新的C++ 11标准添加了一个带有尾随返回类型的新函数声明语法:
// Usual declaration
int foo();
// New declaration
auto foo() -> int;
Run Code Online (Sandbox Code Playgroud)
此语法的优点是可以推导返回类型,如下所示:
template<class T, class U>
auto bar(T t, U u) -> decltype(t + u);
Run Code Online (Sandbox Code Playgroud)
但是为什么返回类型首先放在函数名之前?我想,一个答案是在那个时候不需要这种类型的演绎.如果是这样,假设一种新的编程语言是否有理由不使用尾随返回类型?
我想要将外部库中的函数注释为已弃用,以确保它不会在我的项目中使用.我们假设该库提供以下模块:
module Lib : sig
val safe_function : int -> unit
val unsafe_function : int -> int -> unit
end = struct
let safe_function _ = ()
let unsafe_function _ _ = ()
end
Run Code Online (Sandbox Code Playgroud)
Util.ml我的项目中有一个文件,我在每个文件中打开.在其中,我想做类似的事情:
open Lib
let unsafe_function = Lib.unsafe_function
[@@deprecated "Use safe_function instead."]
let foo = (fun x -> x)
[@@deprecated "BBB"]
type t =
| A [@deprecated]
| B [@deprecated]
[@@deprecated]
Run Code Online (Sandbox Code Playgroud)
编译以下usage.ml文件
open Util
let _ = unsafe_function 0 0
let _ = foo …Run Code Online (Sandbox Code Playgroud) 我们可以使用移动语义在堆栈上移动堆分配的对象吗?
#include <boost/asio.hpp>
#include <memory>
class connection
{
public:
connection(boost::asio::ip::tcp::socket&& socket);
void start();
private:
boost::asio::ip::tcp::socket m_socket;
};
class server
{
public:
// Not relevent here
private:
void accept();
boost::asio::io_service m_io_service;
boost::asio::ip::tcp::acceptor m_acceptor;
};
void server::accept()
{
auto socket = new boost::asio::ip::tcp::socket(m_io_service);
m_acceptor.async_accept(*m_socket,
[&, socket](const boost::system::error_code& error)
{
if (!error)
{
auto connection = std::make_shared<connection>(std::move(*socket));
connection->start();
}
accept();
});
}
Run Code Online (Sandbox Code Playgroud) Benjamin C. Pierce 在其类型和编程语言一书的第23.8节中写道:
系统F的另一个充分研究的限制是由Leivant(1983)引入的rank-2多态性 [...].当类型被绘制为树时,如果没有从其根到∀量词的路径传递到2个或更多箭头的左侧,则称类型为等级2.[...]在rank-2系统中,所有类型都被限制为等级2.这个系统比prenex(ML)片段稍强一些,因为它可以为更多无类型的lambda术语分配类型.
Kfoury和Tiuryn(1990)证明了系统F的秩-2片段的类型重建的复杂性与ML的相同(即,DExptime-complete).Kfoury和Wells(1999)给出了秩2系统的第一个正确的类型重建算法,并且表明系统F的等级3和更高的类型重建是不可判定的.
因此,知道秩-2多态的类型推断是可判定的,并且自1999年以来就已知算法.Haskell支持其RankNTypes语言扩展,但它支持rank-n多态.
哪些语言(如果有的话)实现了rank-2参数多态而不是System F?
由于类型推断是可判定的并且没有最差的复杂性,为什么既没有SML也没有OCaml扩展多态性到-2级?
为什么我们没有在野外看到和阅读更多关于rank-2多态性的信息?
我的地图定义如下:
map<string, LocationStruct> myLocations;其中键是时间字符串
我只在此地图中保留了40个项目,并且当我达到40个项目时,我想放弃地图中的最后一个项目.我知道我做不到myLocations.erase(myLocations.end()),所以我该怎么做呢?
我打算让地图中的最后一项成为最早的项目,因此也是FIFO.数据会很快(大约20Hz)进来,所以我希望地图可以跟上它.我确实需要根据时间查找数据,所以我确实需要它作为关键,但我愿意采用其他方法来实现这一点.
字符串的格式是一个非常详细的"星期六6月21日星期四18:44:21:281",虽然我可以把它简化为简单时代以来的秒数.这是我的第一次尝试,并没有过多考虑格式.
假设我们正在编写一个库,并且我们希望对错误和异常提供精细的控制:
void foo();
void foo(std::error_code&);
Run Code Online (Sandbox Code Playgroud)
我们可以实现foo()抛出a std::system_error并foo(std::error_code&)捕获所有异常并提取error_code.
或者我们应该实现foo(std::error_code&)一个永不抛出的函数,并foo()根据错误代码的存在抛出一个函数?
我需要逐行搜索文件中的两个特定单词,如果它们存在,则打印"Found!".
这是file.txt(有四列)
bill gates 62bill microsoft
beyonce knowles 300mill entertainment
my name -$9000 student
Run Code Online (Sandbox Code Playgroud)
以下是我的想法,但它似乎不起作用
char firstname[];
char lastname[];
char string_0[256];
file = fopen("file.txt","r+");
while((fgets(string_0,256,file)) != NULL) {
//scans the line then sets 1st and 2nd word to those variables
fscanf(file,"%s %s",&firstname, &lastname);
if(strcmp(firstname,"beyonce")==0 && strcmp(lastname,"knowles")==0){
printf("A match has been found");
}
}
fclose(file);
Run Code Online (Sandbox Code Playgroud)
请帮忙.可能是指针没有移动到while循环中的下一行吗?如果是这样,我该如何解决?
我注意到,这auto忽略了双条件。这是一个简化的示例:
Parameter A B : Prop.
Parameter A_iff_B : A <-> B.
Theorem foo1: A -> B.
Proof.
intros H. apply A_iff_B. assumption.
Qed.
Theorem bar1: B -> A.
Proof.
intros H. apply A_iff_B. assumption.
Qed.
Theorem foo2_failing: A -> B.
Proof.
intros H. auto using A_iff_B.
Abort.
Theorem bar2_failing: B -> A.
Proof.
intros H. auto using A_iff_B.
Abort.
Run Code Online (Sandbox Code Playgroud)
现在,我知道这A <-> B是一种语法糖,A -> B /\ B -> A所以我写了两个定理来提取一个或另一个:
Theorem iff_forward : forall {P …Run Code Online (Sandbox Code Playgroud) 我从Coq开始,发现我必须提供一个可判定的平等使用证明List.remove.例如
Require Import Coq.Lists.List.
Import List.ListNotations.
Inductive T : Set := A | B | C.
Lemma eq_dec : forall a b : T, {a = b} + {a <> b}.
Proof.
destruct a, b; try (left; reflexivity); try (right; congruence).
Qed.
Definition f (xs : list T) : list T := List.remove eq_dec A xs.
Run Code Online (Sandbox Code Playgroud)
现在进行类型检查,但我不明白如何使用它.
Theorem foo : f [ A; B; C ] = [ B; C ].
Proof. reflexivity.
Run Code Online (Sandbox Code Playgroud)
给我
Error: Unable to unify …Run Code Online (Sandbox Code Playgroud) 我有时想为现有函数定义一些快捷方式,如下例所示:
Parameter T : Set.
Parameter zero one: T.
Parameter f : T -> T -> option T.
Hypothesis f_unit : forall t, f zero t = None.
Definition g (t : T) := f t one.
Run Code Online (Sandbox Code Playgroud)
但是,这个定义似乎是抽象的,因为我不能f在g没有首先展开的情况下使用关于实例的定理:
Goal (g zero = None).
unfold g.
rewrite f_unit.
reflexivity.
Qed.
Run Code Online (Sandbox Code Playgroud)
有没有办法将定义标记为自动展开?
c++ ×7
c++11 ×5
coq ×3
boost-asio ×2
ml ×2
ocaml ×2
stl ×2
annotations ×1
asynchronous ×1
boost ×1
boost-bind ×1
c ×1
deprecated ×1
erase ×1
file ×1
iostream ×1
map ×1
polymorphism ×1
return-type ×1
sml ×1
std ×1
strcmp ×1
templates ×1
while-loop ×1