在python中,我必须交换2个变量的值,所有你需要做的就是
x,y=y,x
Run Code Online (Sandbox Code Playgroud)
可以看一下它就好像两个语句 - (x = y)和(y = x)并行执行而不是一个接一个地执行.
有没有办法在c ++中实现相同的效果?
注意/编辑:
我希望将这种"并行效果"(如果存在)扩展到更复杂的表达式,如
ones,twos= (ones ^ n) ^ ~twos, (ones & n) | (twos & ~n);
这在python中是可能的,是否可以在c ++中使用?
结论:
所以根据leemes给出的答案和对他答案的评论:
1.您可以在C++ 03或中使用boost库
你可以使用C++ 11
访问std::tie并std::tuple实现这种"并行"效果.至于目前,我将leemes的答案标记为已被接受,但我仍然在寻找在C++ 03中实现这一很酷功能的方法.
我猜测std::hash被定义为模板结构,以避免在重载函数解析期间完成隐式类型转换.这是正确的说法吗?
我的意思是,我更愿意写
std::string s;
size_t hash = std::hash(s);
Run Code Online (Sandbox Code Playgroud)
代替
std::string s;
size_t hash = std::hash<std::string>()(s);
Run Code Online (Sandbox Code Playgroud)
但我猜测标准委员会选择第二种选择的原因是有原因的.
编辑:修复第二个代码片段.
在这里,您可以看到自我分配检查的复制赋值运算符实现:
String & operator=(const String & s)
{
if (this != &s)
{
String(s).swap(*this); //Copy-constructor and non-throwing swap
}
// Old resources are released with the destruction of the temporary above
return *this;
}
Run Code Online (Sandbox Code Playgroud)
这对于自我分配很有用,但对性能有害:
所以,我还是不明白,如果我想实现std::vector的operator=我将如何实现它.
从 C++20 开始,在[namespace.std]/7 中引入了定制点的概念:
除了在命名空间 std 或命名空间 std 内的命名空间中,程序可以为指定为自定义点的任何库函数模板提供重载,前提是 (a) 重载的声明依赖于至少一个用户定义的类型和 (b ) 重载满足自定义点的标准库要求。[ 注意:这允许对自定义点的(限定或非限定)调用为给定参数调用最合适的重载。— 尾注 ]
注释部分(注意强调的词“合格”)是否意味着std::f将自动调用最合适的重载fifstd::f是自定义点?
一个真实的例子是std::swap,这是一个指定的定制点。这是否意味着从 C++20 开始,我们可以std::swap(a, b)直接编写而不是using std::swap; swap(a, b);?
c++ overloading c++-standard-library argument-dependent-lookup c++20
C++是否有内置的部分STL来交换两个数值而不是:
int tmp = var1;
var1 = var2;
var2 = tmp;
Run Code Online (Sandbox Code Playgroud)
像这样的东西:
std::swapValues(var1, var2);
Run Code Online (Sandbox Code Playgroud)
swapValues是一个模板.
我想在我的模板类中重载std :: swap.在以下代码中(简化)
#ifndef Point2D_H
#define Point2D_H
template <class T>
class Point2D
{
protected:
T x;
T y;
public:
Point2D () : x ( 0 ), y ( 0 ) {}
Point2D( const T &x_, const T &y_ ) : x ( x_ ), y ( y_ ) {}
....
public:
void swap ( Point2D <T> &p );
};
template <class T>
inline void swap ( Point2D <T> &p1, Point2D <T> &p2 ) { p1.swap ( p2 ); }
namespace …Run Code Online (Sandbox Code Playgroud) 我正在使用typedef来定义程序中容器的类型,这样我就可以在使用普通STL容器和STXXL容器之间轻松切换,顺序如下:
typedef stxxl:vector<Data> MyContainer;
Run Code Online (Sandbox Code Playgroud)
要么
typedef std:vector<Data> MyContainer;
Run Code Online (Sandbox Code Playgroud)
一个难点是STXXL提供了一个特殊版本std::for_each,stxxl::for_each它针对STXXL容器进行了优化.当MyContainer的typedeffed为a时,我更喜欢使用此函数stxxl::vector.
一种解决方案是定义我自己的for_each函数,该for_each函数调用正确的函数并在我想要调用时使用它for_each.
我正在研究的另一个解决方案是重载/特化,std::foreach以便stxxl::for_each在使用stxxl::vector<Data>::(const_)iterator第一个和第二个参数调用它时调用它.
我不能让第二个想法工作.我尝试过以下方法:
namespace std
{
template <class UnaryFunction>
UnaryFunction for_each(stxxl:vector<Data>::const_iterator first,
stxxl:vector<Data>::const_iterator last, UnaryFunction f)
{
stxxl::for_each(first, last, f, 4);
}
}
Run Code Online (Sandbox Code Playgroud)
与非const迭代器的类似函数一起使用.虽然他们没有被召唤.
这个问题的首选解决方案是什么?我怎样才能让我的版本std::for_each为stxxl::vector得到所谓的迭代器?
更新:我发布了第二个想法.问题是我包含了错误的文件(哎呀......).但问题仍然存在:这个问题的首选解决方案是什么?是否可以重载std :: for_each,因为std命名空间不适合凡人?
请考虑以下代码:
#include <algorithm>
#include <iostream>
#include <vector>
struct A {
int val;
bool operator<(const A& other) const {
std::cout << "operator\n";
return val < other.val;
}
};
void swap(A& a, A& b) {
std::cout << "foo\n";
std::swap(a.val, b.val);
}
int main()
{
std::vector<A> a(2);
a[0].val = 10;
a[1].val = -1;
std::sort(a.begin(), a.end());
}
Run Code Online (Sandbox Code Playgroud)
C++ 11的std::sort地方ValueSwappable的迭代器参数的要求,移动语义,没有别的,这意味着std::sort是"保证",如果内容需要四处走动,进行互换.并且17.6.3.2/3暗示在这种情况下我的过载肯定应该被选中.
clang 3.1 SVN的libc ++选择我swap(也就是说,我看到"foo"); GCC 4.6.3的libstdc ++没有.
我上课了
class Zaposlenik {
private:
string prezime;
string funkcija;
double placa;
public:
bool operator==(const string& prezime) const;
bool operator<(const string &prezime) const;
bool operator<(const Zaposlenik &other) const;
Run Code Online (Sandbox Code Playgroud)
我使用带字符串的运算符进行二进制搜索,使用运算符<和Zaposlenik进行排序
我无法更改头文件类我只能在.cpp中编写代码.
我也有
class Firma {
private:
vector<Zaposlenik> zaposlenici;
public:
void sort();
Run Code Online (Sandbox Code Playgroud)
我也不能改变那个类,我必须为它写.cpp.我将2 .cpp上传到自动评分服务器,该服务器将500 000 Zaposlenik输入到矢量zaposlenici,然后进行2 000 000次搜索.
我使用qsort和bsearch而且速度太慢了.我上传它时不能超过3秒.
我写了重载的运算符,我相信它们很好,但显然qsort可以更快.
矢量按字符串prezime排序,名称从"aaaa"到"ZZZZ"所以4个字母组合大小写字母.
string funkcija;而double placa; 并不意味着排序什么.
有人能告诉我哪种比qsort快吗?请记住,我对主要内容没有任何控制权,因此我无法统计成员.
PS在类中还有其他功能,但它们对此部分没有任何意义.还有Bsearch的功能,但这个功能和我相信的一样快.
我在C++中包含头文件时遇到问题.据我所知,放入using namespace std标题并不是一个好的设计,但是当我尝试删除它时出现了一些错误.这是头文件中的代码:
#include <iostream>
#include <string>
//using namespace std;
class Messages
{
public:
Messages(string sender, string recipient,int time);
void append();
string to_string();
private:
int time;
string sender;
string recipient;
string text;
};
Run Code Online (Sandbox Code Playgroud)
我确实包含了<string>.但是,如果我不使用namespace std,那么我的所有字符串都会显示错误.我不想添加using namespace std头文件,因为它是一个糟糕的设计.那么我该如何解决呢?
提前致谢.
编码
using namespace std;
class A
{
private:
vector<int> a;
public:
A(vector<int> x):a(x){}
string toString()
{
string s;
for (auto& element : a)
{
s += to_string(element) + " ";
}
return s;
}
};
int main()
{
A a1({1,2,3});
A a2({11,12,13});
cout << "a1 = " << a1.toString() << "\n";
cout << "a2 = " << a2.toString() << "\n";
swap(a1,a2);
cout << "a1 = " << a1.toString() << "\n";
cout << "a2 = " << a2.toString() << "\n";
return …Run Code Online (Sandbox Code Playgroud) 我想为基本类型/对象的std :: vector重载交换函数.原因是使用std :: sort对包含大对象的向量进行了慢速排序.这是一个简单但不起作用的例子.
#include <vector>
#include <algorithm>
class Point
{
private:
double x, y;
public:
Point(double xx, double yy) : x(xx), y(yy) {}
bool operator < ( const Point& p ) const
{
return x < p.x;
}
void swap(Point &p)
{
std::swap(*this, p);
}
};
namespace std
{
void swap( Point &p1, Point &p2)
{
p1.swap(p2);
}
}
typedef std::vector<Point> TPoints;
int main()
{
Point p1(0,0);
Point p2(7,100);
TPoints points;
points.push_back(p1);
points.push_back(p2);
//Overloaded metod swap will not …Run Code Online (Sandbox Code Playgroud) 写了我自己的数字类。超载std::numeric_limit::max()就好。然而,我正在尝试超载std::abs()但编译器不喜欢我的尝试。我试过这个:
namespace std \n{\n template<uint8_t T> \n MyType<T> abs(const MyType<T>& mt)\n {\n MyType<T> copy = mt;\n copy.val = std::abs(md.val);\n return copy;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n但是,编译器没有发现重载:
\nerror: no matching function for call to \xe2\x80\x98abs(const MyType<10>&)\xe2\x80\x99\n/usr/include/c++/11/bits/std_abs.h:56:3: note: candidate: \xe2\x80\x98long int std::abs(long int)\xe2\x80\x99\n 56 | abs(long __i) { return __builtin_labs(__i); }\n | ^~~\n/usr/include/c++/11/bits/std_abs.h:56:12: note: no known conversion for argument 1 from \xe2\x80\x98const MyType<10>\xe2\x80\x99 to \xe2\x80\x98long int\xe2\x80\x99\n 56 | abs(long __i) { return __builtin_labs(__i); }\n | ~~~~~^~~\n/usr/include/c++/11/bits/std_abs.h:61:3: note: candidate: …Run Code Online (Sandbox Code Playgroud)