这里发生了什么事?
#include <iostream>
using namespace std;
int main(){
int x=0,y=0;
true? ++x, ++y : --x, --y;
cout << "x: " << x << endl;
cout << "y: " << y << endl; //why does y=0 here?
x=0,y=0;
false ? ++x, ++y : --x, --y;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
}
x: 1
y: 0
x: -1
y: -1
Run Code Online (Sandbox Code Playgroud)
第二种情况似乎很好.我希望x和y在第一种情况下增加到1,但只有左手操作数增加.
对于std::unique_ptrs p1和p2,std::move()和之间有什么区别std::unique_ptr::reset()?
p1 = std::move(p2);
p1.reset(p2.release());
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用boost :: thread将C++ 11 std :: thread代码移植到VC9(VS 2008).下面的'等效'C++ 11代码在msvc12上编译得很好:
#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
#include <cassert>
void thFun(int i)
{
std::cout << "Hello from thread " << i << " !\n";
}
int main()
{
std::vector<std::thread> workers;
for (int i = 0; i < 10; ++i)
{
auto th = std::thread(&thFun, i);
workers.push_back(std::move(th));
assert(!th.joinable());
}
std::cout << "Hello from main!\n";
std::for_each(workers.begin(), workers.end(), [](std::thread& th)
{
assert(th.joinable());
th.join();
});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想使用msvc9编译器和Boost 1.55将代码移植到C++ 03.如何解决以下编译错误:
#include <iostream>
#include …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试返回类型__repr__.它不是一个字符串,它是什么?这里发生了什么事?
import unittest
class MyClass(unittest.TestCase):
class Dog(object):
def __init__(self, initial_name):
self._name = initial_name
def get_self(self):
return self
def __repr__(self):
return "Dog named '" + self._name + "'"
def runTest(self):
fido = self.Dog("Fido")
self.assertEqual("Dog named 'Fido'", fido.get_self()) #Fails!
test=MyClass("runTest")
runner=unittest.TextTestRunner()
runner.run(test)
Run Code Online (Sandbox Code Playgroud)
运行这个给出:
FAIL: runTest (__main__.MyClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/xxxxx/fido.py", line 15, in runTest
self.assertEqual("Dog named 'Fido'", fido.get_self())
AssertionError: "Dog named 'Fido'" != Dog named 'Fido'
----------------------------------------------------------------------
Ran 1 test in 0.006s
FAILED (failures=1)
Run Code Online (Sandbox Code Playgroud)
我怎样才能通过这项测试?
为什么可以定义vector :: iterator to int的map,但是list :: iterator to int的map不能?
#include <vector>
#include <list>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
int ia[] = {1,2,3,4,5,6,7,8,9,0};
vector<int> v(begin(ia), end(ia));
auto it1 = find(begin(v), end(v), 4);
map< vector<int>::const_iterator, int > m1;
m1.insert(map<vector<int>::const_iterator, int>::value_type(it1,*it1));
list<int> l(begin(ia), end(ia));
auto it2 = find(begin(l), end(l),5);
map< list<int>::const_iterator, int> m2;
m2.insert(map<list<int>::const_iterator, int>::value_type(it2,*it2)); //doesn't compile
}
Run Code Online (Sandbox Code Playgroud)
错误1错误C2678:二进制'<':找不到运算符,它接受类型为'const std :: _ List_const_iterator <_Mylist>'的左操作数(或者没有可接受的转换)
我正在尝试使用std :: remove_reference.例如,我可以提取元素类型数组但是如何使用remove_reference来处理STL容器?例如,我想使用下面的remove_reference将迭代器返回到向量的元素:
#include <iostream>
#include <vector>
#include <type_traits>
using std::vector;
using std::cout;
using std::endl;
using std::begin;
using std::end;
using std::remove_reference;
template<typename T>
auto my_end(T& c) -> typename remove_reference<decltype(&c[0])>::type
{
return end(c)-1; //compile error when myend<vector> is instantiated
}
int main()
{
int ia[] = {1,2,3,4,5,6,7,8,10};
vector<int> v(begin(ia), end(ia));
auto my_back1 = *my_end(ia);
cout << my_back1 << endl; //prints 10
auto my_back2 = *my_end(v);
cout << my_back2 << endl; //should print 10
}
Run Code Online (Sandbox Code Playgroud)
my_end<vector>实例化时的编译器错误是:
cannot convert from 'std::_Vector_iterator<_Myvec>' to …Run Code Online (Sandbox Code Playgroud) 由于const int专业化导致以下错误:
#include <iostream>
using std::cout;
using std::endl;
template <typename T> void g(T val)
{
cout << "unknown" << endl;
}
template <> void g(int && val)
{
cout << "int &&" << endl;
}
template <> void g(const int && val)
{
cout << "const int &&" << endl;
}
template <> void g(int & val)
{
cout << "int &" << endl;
}
template <> void g(const int & val)
{
cout << "const int &" …Run Code Online (Sandbox Code Playgroud) c++ ×6
c++11 ×3
boost ×1
boost-move ×1
boost-thread ×1
comma ×1
python ×1
stdmap ×1
unique-ptr ×1
unit-testing ×1
visual-c++ ×1