我刚读了一些关于使用的建议
std::string s = get_string();
std::string t = another_string();
if( !s.compare(t) )
{
Run Code Online (Sandbox Code Playgroud)
代替
if( s == t )
{
Run Code Online (Sandbox Code Playgroud)
我几乎总是使用最后一个,因为我已经习惯了它,它感觉自然,更具可读性.我甚至不知道有一个单独的比较功能.更确切地说,我认为==会调用compare().
有什么区别?在哪种情况下,一种方式应该受到另一种方式的青睐?
我只考虑我需要知道字符串是否与另一个字符串相同的情况.
我一直将其精简为仅使用Boost Operators:
#include <boost/operators.hpp>
struct F : boost::totally_ordered1<F, boost::totally_ordered2<F, int>> {
/*implicit*/ F(int t_) : t(t_) {}
bool operator==(F const& o) const { return t == o.t; }
bool operator< (F const& o) const { return t < o.t; }
private: int t;
};
int main() {
#pragma GCC diagnostic ignored "-Wunused"
F { 42 } == F{ 42 }; // OKAY
42 == F{42}; // C++17 OK, C++20 infinite …
Run Code Online (Sandbox Code Playgroud)例如,在下面的主分支中,我需要删除提交af5c7bf16e6f04321f966b4231371b21475bc4da,这是由于之前的rebase导致的第二个:
commit 60b413512e616997c8b929012cf9ca56bf5c9113
Author: Luca G. Soave <luca.soave@gmail.com>
Date: Tue Apr 12 23:50:15 2011 +0200
add generic config/initializers/omniauth.example.rb
commit af5c7bf16e6f04321f966b4231371b21475bc4da
Author: Luca G. Soave <luca.soave@gmail.com>
Date: Fri Apr 22 00:15:50 2011 +0200
show github user info if logged
commit e6523efada4d75084e81971c4dc2aec621d45530
Author: Luca G. Soave <luca.soave@gmail.com>
Date: Fri Apr 22 17:20:48 2011 +0200
add multiple .container at blueprint layout
commit 414ceffc40ea4ac36ca68e6dd0a9ee97e73dee22
Author: Luca G. Soave <luca.soave@gmail.com>
Date: Thu Apr 21 19:55:57 2011 +0200
add %h1 Fantastic Logo + .right for 'Sign …
Run Code Online (Sandbox Code Playgroud) 使用http://en.cppreference.com/w/cpp/string/basic_string_view作为参考,我认为没有办法更优雅地做到这一点:
std::string s = "hello world!";
std::string_view v = s;
v = v.substr(6, 5); // "world"
Run Code Online (Sandbox Code Playgroud)
更糟糕的是,天真的方法是一个陷阱,并留下v
暂时的悬挂参考:
std::string s = "hello world!";
std::string_view v(s.substr(6, 5)); // OOPS!
Run Code Online (Sandbox Code Playgroud)
我似乎记得有些东西可能会添加到标准库中以返回子字符串作为视图:
auto v(s.substr_view(6, 5));
Run Code Online (Sandbox Code Playgroud)
我可以想到以下解决方法:
std::string_view(s).substr(6, 5);
std::string_view(s.data()+6, 5);
// or even "worse":
std::string_view(s).remove_prefix(6).remove_suffix(1);
Run Code Online (Sandbox Code Playgroud)
坦率地说,我认为这些都不是很好.现在我能想到的最好的事情是使用别名来简化事情.
using sv = std::string_view;
sv(s).substr(6, 5);
Run Code Online (Sandbox Code Playgroud) 这个问题可能很愚蠢,但我一直想知道它.这是关于git存储库,但我认为它对于其他DVCS的本地存储库是相同的.
让我们说我的项目在启动时是这样的:
那么当你设置它时,它是如何工作的?
让我说我将Project文件夹移动到其他地方,我是否需要更改任何内容?或者.git文件夹中的所有存储库内容仅相对于Project文件夹,忽略Project上方的整个文件树.
我很确定移动Project无所谓,但我只是想确定.
此代码示例无法编译.有什么工作吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
using church = Func<dynamic, dynamic, dynamic>;
class Program
{
static void Main(string[] args)
{
church True = (a, b) => a;
church False = (a, b) => b;
Func<church, church, church> And = (x, y) => x(y(True, False), False);
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误6内部编译器错误(地址5476A4CC处的0xc0000005):可能的罪魁祸首是'EMITIL'.编译器中发生内部错误.若要解决此问题,请尝试简化或更改下面列出的位置附近的程序.列表顶部的位置更接近发生内部错误的位置.可以使用/ errorreport选项向Microsoft报告此类错误.TestApplication
令我惊讶的是,我遇到了另一个障碍,例如C++20 行为用相等运算符破坏了现有代码?.
考虑一个简单的不区分大小写的键类型,用于例如std::set
or std::map
:
// Represents case insensitive keys
struct CiKey : std::string {
using std::string::string;
using std::string::operator=;
bool operator<(CiKey const& other) const {
return boost::ilexicographical_compare(*this, other);
}
};
Run Code Online (Sandbox Code Playgroud)
简单的测试:
using KeySet = std::set<CiKey>;
using Mapping = std::pair<CiKey, int>; // Same with std::tuple
using Mappings = std::set<Mapping>;
int main()
{
KeySet keys { "one", "two", "ONE", "three" };
Mappings mappings {
{ "one", 1 }, { "two", 2 }, { "ONE", 1 }, { "three", …
Run Code Online (Sandbox Code Playgroud) 在Bash脚本中,是否可以在"尚未使用的编号最小的文件描述符"上打开文件?
我四处寻找如何做到这一点,但似乎Bash总是要求你指定数字,例如:
exec 3< /path/to/a/file # Open file for reading on file descriptor 3.
Run Code Online (Sandbox Code Playgroud)
相反,我希望能够做类似的事情
my_file_descriptor=$(open_r /path/to/a/file)
Run Code Online (Sandbox Code Playgroud)
这将打开'file'以读取尚未使用的编号最小的文件描述符,并将该编号分配给变量'my_file_descriptor'.
我一直在寻找更多适合的习语std::exchange
。
今天我发现自己在一个答案中写下了这个:
do {
path.push_front(v);
} while (v != std::exchange(v, pmap[v]));
Run Code Online (Sandbox Code Playgroud)
我比说更喜欢它
do {
path.push_front(v);
if (v == pmap[v])
break;
v= pmap[v];
} while (true);
Run Code Online (Sandbox Code Playgroud)
希望有明显的原因。
然而,我对标准语言并不热衷,我不禁担心这并lhs != rhs
不能保证右侧表达式在左侧表达式之前没有被完全求值。这将使其成为同义反复的比较 - 根据定义将返回true
.
然而,代码确实运行正确,显然lhs
首先进行评估。
有人知道吗
附言。f(a,b)
我意识到这是where f
is的一个特例operator!=
。我尝试使用此处找到的信息回答我自己的查询,但迄今为止未能得出结论:
了解一下Haskell有一个关于仿函数的例子.我可以阅读LYAH和文本,并弄清楚应该发生什么 - 但我不知道写这样的东西.我经常在Haskell中发现这个问题.
instance Functor (Either a) where
fmap f (Right x) = Right (f x)
fmap f (Left x) = Left x
Run Code Online (Sandbox Code Playgroud)
但是,我很困惑..为什么不这个补充
instance Functor (Either a) where
fmap f (Right x) = Right (x)
fmap f (Left x) = Left (f x)
Run Code Online (Sandbox Code Playgroud)
如果f
没有在顶部定义中使用,那么还有什么限制x
使得它无法满足Left