像Java这样的Python中有什么东西StringBuffer
吗?由于字符串在Python中也是不可变的,因此在循环中编辑它们效率很低.
我正在编写一个Python脚本,我想要批量上传照片.我想读取一个Image并将其转换为字节数组.任何建议将不胜感激.
#!/usr/bin/python
import xmlrpclib
import SOAPpy, getpass, datetime
import urllib, cStringIO
from PIL import Image
from urllib import urlopen
import os
import io
from array import array
""" create a proxy object with methods that can be used to invoke
corresponding RPC calls on the remote server """
soapy = SOAPpy.WSDL.Proxy('localhost:8090/rpc/soap-axis/confluenceservice-v2?wsdl')
auth = soapy.login('admin', 'Cs$corp@123')
Run Code Online (Sandbox Code Playgroud) 在整个网络上,我看到人们使用擦除/删除习惯用于C++向量,如下所示:
#include <vector> // the general-purpose vector container
#include <iostream>
#include <algorithm> // remove and remove_if
int main()
{
// initialises a vector that holds the numbers from 0-9.
std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// removes all elements with the value 5
v.erase( std::remove( v.begin(), v.end(), 5 ), v.end() );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
也就是说,如果我想要擦除符合某些条件的所有元素(例如,来自int
s 向量的数字5 ),那么我使用std::remove
或std::remove_if
结合使用vector.erase
如下:
vector.erase( std::remove( vector.begin(), vector.end(), …
Run Code Online (Sandbox Code Playgroud) 我很好奇背后的理由noexcept
在的C++ 0x FCD.throw(X)
被弃用了,但noexcept
似乎做了同样的事情.有noexcept
没有在编译时未检查的原因?看起来如果静态检查这些函数它们只会在一个try
块中调用抛出函数会更好.
enum
C++中的类型是相当基本的; 它基本上只是为标签创建了一堆编译时值(可能具有适当的范围enum class
).
将相关的编译时常量分组在一起非常有吸引力:
enum class Animal{
DOG,
CAT,
COW,
...
};
// ...
Animal myAnimal = Animal::DOG;
Run Code Online (Sandbox Code Playgroud)
然而,它有各种各样的缺点,包括:
在这篇文章中,我试图创建一种解决这些缺点的类型.
一个理想的解决方案采用常量的编译时知识及其与字符串的关联的概念,并将它们组合成一个类似于scoped-enum的对象,可以通过enum id和enum string name进行搜索.最后,结果类型将使用尽可能接近枚举语法的语法.
在这篇文章中,我将首先概述其他人为各个部分所尝试的内容,然后介绍两种方法,一种完成上述方法,但由于静态成员的初始化顺序而具有未定义的行为,另一种解决方案不那么漂亮语法但由于初始化顺序没有未定义的行为.
关于获取枚举中的项目数量(1 2 3)以及网上提出的大量其他问题(4 5 6)等,有很多问题.而且普遍的共识是,没有确定 -火的方式做到这一点.
仅当您强制执行枚举值为正且增加时,以下模式才有效:
enum Foo{A=0, B, C, D, FOOCOUNT}; // FOOCOUNT is 4
Run Code Online (Sandbox Code Playgroud)
但是,如果您尝试编码某种需要任意值的业务逻辑,则很容易被破坏:
enum Foo{A=-1, B=120, C=42, D=6, FOOCOUNT}; // ????
Run Code Online (Sandbox Code Playgroud)
所以Boost的开发人员试图用Boost.Enum来解决这个问题,Boost.Enum使用了一些相当复杂的宏来扩展到一些至少会给你大小的代码.
在迭代枚举中有一些尝试; 可以迭代的类似枚举的对象,理论上允许隐式大小计算,甚至在[7](7 8 9,...)的情况下明确允许
尝试实现这一点通常会导致自由浮动函数和使用宏来适当地调用它们.(8 …
我一直在玩内部课程C++
,现在我有点困惑.
#include <iostream>
class outer{
private:
class inner{
private:
int something;
public:
void print(){
std::cout<< "i am inner"<<std::endl;
}
};
public:
inner returnInner(){
inner i;
return i;
}
};
int main(){
outer o;
//outer::inner i = o.returnInner(); (1)
//auto i = o.returnInner(); (2)
//i.print();
o.returnInner().print(); //(3)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是编译在Linux clang++-3.5
和-std=c++14
.
在(1)中我得到了预期的编译器错误,因为它inner
是一个私有的内部类outer
.
但是,在(2)auto
中使用关键字时,编译成功并运行程序.
为什么会这样?我是否可以阻止从外部方法返回私有内部类的实例,同时保留在外部类中移动和/或复制它们的能力?
编译器不应该像在(1)中那样在(2)和(3)中引出错误吗?
假设我有两个结构,Foo
并且Bar
:
template<int...>
struct Foo{};
template<unsigned long...>
struct Bar{};
Run Code Online (Sandbox Code Playgroud)
我想创建一个类型特征(调用它match_class
),如果我传递两种Foo<...>
类型或两种Bar<...>
类型,则返回true ,但如果我尝试混合它们则返回false:
int main()
{
using f1 = Foo<1, 2, 3>;
using f2 = Foo<1>;
using b1 = Bar<1, 2, 3>;
using b2 = Bar<1>;
static_assert(match_class<f1, f2>::value, "Fail");
static_assert(match_class<b1, b2>::value, "Fail");
static_assert(!match_class<f1, b1>::value, "Fail");
}
Run Code Online (Sandbox Code Playgroud)
对于C++ 1z(clang 5.0.0和gcc 8.0.0),它就足够了(Demo):
template<class A, class B>
struct match_class : std::false_type{};
template<class T, template<T...> class S, T... U, T... V>
struct match_class<S<U...>, S<V...>> : …
Run Code Online (Sandbox Code Playgroud) [temp.constr.decl]说我们可以使用约束表达式来约束模板或函数。
声明符[dcl.decl]告诉我们,对于函数,我们可以添加可选的尾随require 子句来约束它,标准草案n4820甚至提供了这些(看似毫无意义的)示例:
void f1(int a) requires true;
auto f2(int a) -> bool requires true;
Run Code Online (Sandbox Code Playgroud)
我知道约束模板或概念很有用,但是我看不到这些约束对非模板函数有何用处。约束非模板函数有什么意义?
为什么operator ()
不允许无状态仿函数static
?无状态lambda对象可转换为指向具有与其相同签名的自由函数的指针operator ()
.
Stephan T. Lavavej on p.6指出转换为函数指针只是一个operator FunctionPointer()
(引用).但是我无法获得与operator ()
非成员函数相对应的指针.对于仿函数struct F { void operator () () {} }
,似乎无法转换&F::operator ()
为类型的实例using P = void (*)();
.
码:
struct L
{
static
void operator () () const {}
operator auto () const
{
return &L::operator ();
}
};
Run Code Online (Sandbox Code Playgroud)
错误是
重载'operator()'不能是静态成员函数
但operator ()
没有超载.
我使用vs14编译器的函数模板有问题.因此,以下代码演示了该问题.
#include <iostream>
using namespace std;
class Class {
public:
int memberFoo() {
return 0;
}
};
template <class VariableT, class C>
void nothing(const VariableT C::*memberV) {
cout << "Pointer to member variable";
}
template <class R, class C>
void nothing(R (C::*memberF)()) {
cout << "Pointer to member function";
}
int main() {
nothing(&Class::memberFoo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器让我知道nothing
函数是模糊的.当我看到输出时,它似乎有其他行为超出我的预期.在第一个nothing
函数中,编译器推导VariableT
为int(void)
.实际上并不奇怪,但我认为第二个更合适并且会匹配.更有意思的是,如果const
在第一个重载函数中删除,程序将正确编译.你能建议我怎么处理这个吗?