C++ 在C++;?
可能重复:C++ 在C++;?,c++,python,C++,Python,可能重复: 昨天,当我写这篇文章的时候,有人问我 如果我有一个字符串x='wow' 在python中应用函数add: x='wow' x.add(x) 'wowwow' 如何在C++中实现这一点?< /P> 将add(不存在)更正为\uuuu add\uuuu(标准 方法)这是一个深刻而有趣的问题,涉及到两个微妙的方面 级别详细信息、高级别算法复杂性注意事项,以及 甚至线程!,但它的表述非常简短 我正在转载 因为我自己没有机会提供正确的答案 在删除之前回答,我努力让原来的问题重新出现,所以
昨天,当我写这篇文章的时候,有人问我 如果我有一个字符串
x='wow'
在python中应用函数add
:
x='wow'
x.add(x)
'wowwow'
如何在C++中实现这一点?< /P>
将add
(不存在)更正为\uuuu add\uuuu
(标准
方法)这是一个深刻而有趣的问题,涉及到两个微妙的方面
级别详细信息、高级别算法复杂性注意事项,以及
甚至线程!,但它的表述非常简短
我正在转载
因为我自己没有机会提供正确的答案
在删除之前回答,我努力让原来的问题重新出现,所以
我认为我可以帮助提高对这些问题的普遍理解,但失败了
< >我把原来的标题“选择Python或C++”改为…
- C++中的等价于cPython字符串的连接?
x = 'wow'
x.__add__( x )
在Python2.x和Python3.x中有不同的含义
在Python2.x中,字符串默认为窄字符串,每个编码单元有一个字节,
对应于C++ <代码> char < /C>字符串。
在Python 3.x中,字符串是宽字符串,保证表示Unicode,
对应于C++的实际使用
wchar\u t
基于字符串,同样使用未指定的2或4字节
每个编码单元
不考虑效率,\uuuuu添加
方法在两个主节点中的行为相同
Python版本,对应于C++ <代码> +//>运算符>代码> STD::Basic字符串
(即,对于std::string
和std::wstring
),例如引用:
对象。\uuuu添加(自我、其他)
…要计算表达式
x+y
,其中x
是具有\uuuu add\uuuu()
方法的类的实例,将调用x.\uu add\uuuuu(y)
例如,CPython2.7代码
x = 'wow'
y = x.__add__( x )
print y
通常写为
x = 'wow'
y = x + x
print y
对应于C++代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
auto const x = string( "wow" );
auto const y = x + x;
cout << y << endl;
}
或者就这件事而言
x = x + a + b + c
被替换为
x += a
x += b
x += c
在一般情况下,对x
引用,并且由于Python字符串对象必须看起来是不可变的
更新分配无法更改该字符串对象。因此,一般来说,它具有
创建一个全新的字符串对象,并将该(引用)指定给x
此时,x
保存对该对象的唯一引用。这意味着
对象可以通过追加b
的更新分配进行更新,因为
没有观察员。对于c
的追加也是如此
这有点像量子力学:你不能观察这个肮脏的东西
如果有人可能观察到的话,这是永远不会发生的
这些阴谋,但你可以从统计数据推断,这一定是在进行
你收集关于性能的信息,因为线性时间和二次时间是完全不同的
如何实现线性时间?嗯,随着更新,缓冲区的策略也是一样的
与C++中的加倍一样:STD::Basic字符串可以完成,这意味着
现有缓冲区内容只需在每次缓冲区重新分配时复制,
而不是针对每个追加操作。这意味着
复制的总成本在最坏情况下,在最终字符串大小中是线性的
与总和相同(表示每次缓冲区翻倍时的复制成本)
1+2+4+8+…+N小于2*N
C++中的线性时间串连接表达式。
为了忠实地复制C++中的cPython代码片段,
- 应捕获操作的最终结果和表达式性质
- 并且应该捕捉到它的性能特征
对于性能特性C++ +Cux>+= 是基本的答案,但是,这确实是 无法捕获Python代码的表达式性质
自然的答案是一个线性时间C++ +String > String Builder <强>类 一个连接表达式到一系列的
+=
更新,这样Python代码
from __future__ import print_function
def foo( s ):
print( s )
a = 'alpha'
b = 'beta'
c = 'charlie'
foo( a + b + c ) # Expr-like linear time string building.
大致相当于
#include <string>
#include <sstream>
namespace my {
using std::string;
using std::ostringstream;
template< class Type >
string stringFrom( Type const& v )
{
ostringstream stream;
stream << v;
return stream.str();
}
class StringBuilder
{
private:
string s_;
template< class Type >
static string fastStringFrom( Type const& v )
{
return stringFrom( v );
}
static string const& fastStringFrom( string const& s )
{ return s; }
static char const* fastStringFrom( char const* const s )
{ return s; }
public:
template< class Type >
StringBuilder& operator<<( Type const& v )
{
s_ += fastStringFrom( v );
return *this;
}
string const& str() const { return s_; }
char const* cStr() const { return s_.c_str(); }
operator string const& () const { return str(); }
operator char const* () const { return cStr(); }
};
} // namespace my
#include <iostream>
using namespace std;
typedef my::StringBuilder S;
void foo( string const& s )
{
cout << s << endl;
}
int main()
{
string const a = "alpha";
string const b = "beta";
string const c = "charlie";
foo( S() << a << b << c ); // Expr-like linear time string building.
}
#包括
#包括
名称空间我的{
使用std::string;
使用std::ostringstream;
模板<类类型>
字符串stringFrom(const&v类型)
{
ostringstream;
流动
静态字符串fastStringFrom(const&v类型)
{
从(v)返回;
}
静态字符串常量和fastStringFrom(字符串常量和s)
{返回s;}
静态字符常量*fastStringFrom(字符常量*常量s)
{返回s;}
公众:
模板<类类型>
StrugBuudio&PuffigaMo,原问题应该(并且在10分钟内可能会被删除)。当问到最初的问题时,它是沿着“我如何连接C++中的字符串”的语调?用Python例子来帮助解释。在C++中,字符串是可变的。被问及的方式,我认为不可变性应该是重要的。@ Brand这个,虽然有趣,但决不是一个理由来删除原始问题。没有提到CO。
#include <string>
#include <sstream>
namespace my {
using std::string;
using std::ostringstream;
template< class Type >
string stringFrom( Type const& v )
{
ostringstream stream;
stream << v;
return stream.str();
}
class StringBuilder
{
private:
string s_;
template< class Type >
static string fastStringFrom( Type const& v )
{
return stringFrom( v );
}
static string const& fastStringFrom( string const& s )
{ return s; }
static char const* fastStringFrom( char const* const s )
{ return s; }
public:
template< class Type >
StringBuilder& operator<<( Type const& v )
{
s_ += fastStringFrom( v );
return *this;
}
string const& str() const { return s_; }
char const* cStr() const { return s_.c_str(); }
operator string const& () const { return str(); }
operator char const* () const { return cStr(); }
};
} // namespace my
#include <iostream>
using namespace std;
typedef my::StringBuilder S;
void foo( string const& s )
{
cout << s << endl;
}
int main()
{
string const a = "alpha";
string const b = "beta";
string const c = "charlie";
foo( S() << a << b << c ); // Expr-like linear time string building.
}