这是C++ Primer第5版的练习:
练习16.27:对于每个带标签的语句,解释实例化发生的情况(如果有的话).如果模板被实例化,请解释原因; 如果没有,请解释原因.P.677
template <typename T> class Stack { };
void f1(Stack<char>); // (a)
class Exercise {
Stack<double> &rsd; // (b)
Stack<int> si; // (c)
};
int main() {
Stack<char> *sc; // (d)
f1(*sc); // (e)
int iObj = sizeof(Stack< string >); // (f)
}
Run Code Online (Sandbox Code Playgroud)
以下是我的尝试:
(a)Stack<char>被实例化,但没有实例化它的成员.
(b)Stack<double>被实例化,但没有实例化其成员.
(c)Stack<int>并实例化其默认构造函数.
(d)(e)完全不知道......
(f)Stack< string >被实例化,但没有实例化其成员.
我对吗?谁能告诉我这个代码是如何实例化的?
我正在使用C++ Primer第5版进行练习,如下所示:
练习13.50:将print语句放在String类的移动操作中, 并从第13.6.1节(第534页)中的练习13.48重新运行程序,该练习使用向量来查看何时避免复制.(P.544)
这String是一个练习的类,其行为类似于std::string不使用任何模板.该String.h文件中:
class String
{
public:
//! default constructor
String();
//! constructor taking C-style string i.e. a char array terminated with'\0'.
explicit String(const char * const c);
//! copy constructor
explicit String(const String& s);
//! move constructor
String(String&& s) noexcept;
//! operator =
String& operator = (const String& rhs);
//! move operator =
String& operator = (String&& rhs) noexcept;
//! destructor
~String();
//! members
char* …Run Code Online (Sandbox Code Playgroud) 下面的代码编译时没有错误:
std::string lastName, chldName;
while([&]()
{
return true;
})
{
//codes...
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这种方式时:
std::string lastName, chldName;
while([&]()
{
std::cin >>lastName;
return true;
})
{
//codes...
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨说:
错误:无法将'main():: {(*&lastName)}'从'main()::'转换为'bool'
如何理解这个错误?是否可以这样使用lambda?
昨天我做了很多事情,比如更新GCC,Clang和重新安装Qt Creator.今天在逐步调试我的代码时,调试器显示的是反汇编程序而不是我编写的C++代码.按F10或F11,调试器在反汇编移动不是.cpp也.h文件我写的.F11只能进入库文件,但永远不会进入我写的文件.
箭头出现在反汇编程序中:

而不是在main.cpp中:

如何配置Qt Creator使调试箭头跟踪C++代码中的每一行?
这是C++ Primer第5版的练习:
练习13.53:作为低效率的问题,HasPtr 赋值运算符并不理想.解释为什么.为HasPtr实现一个复制赋值和移动赋值运算符,并比较新移动赋值运算符和复制交换版本中执行的操作.(P.544)
档案hasptr.h:
//! a class holding a std::string*
class HasPtr
{
friend void swap(HasPtr&, HasPtr&);
friend bool operator <(const HasPtr& lhs, const HasPtr& rhs);
public:
//! default constructor.
HasPtr(const std::string &s = std::string()):
ps(new std::string(s)), i(0)
{ }
//! copy constructor.
HasPtr(const HasPtr& hp) :
ps(new std::string(*hp.ps)), i(hp.i)
{ }
//! move constructor.
HasPtr(HasPtr&& hp) noexcept :
ps(hp.ps), i(hp.i)
{ hp.ps = nullptr; }
//! assignment operator
HasPtr&
operator = …Run Code Online (Sandbox Code Playgroud) c++ rvalue-reference assignment-operator move-semantics c++11
这是代码:
#include <string>
#include <regex>
#include <iostream>
int main()
{
std::string pattern("[^c]ei");
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
std::regex r(pattern);
std::smatch results;
std::string test_str = "cei";
if (std::regex_search(test_str, results, r))
std::cout << results.str() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
cei
Run Code Online (Sandbox Code Playgroud)
使用的编译器是gcc 4.9.1.
我是一个学习正则表达式的新手.我期望什么都不应该输出,因为"cei"这里的模式不匹配.我做得对吗?有什么问题?
更新:
这个已被报告并确认为错误,详情请访问:https: //gcc.gnu.org/bugzilla/show_bug.cgi?id = 63497
我试图定义一个运算符=>>来检查它的一个操作数是否是另一个操作数的两倍。
我到目前为止尝试过:
:- op(200, xfy, =>>).
=>>(L, R) :- double(L, R); double(R, L).
double(L, R) :- L is R * 2.
Run Code Online (Sandbox Code Playgroud)
但是当在 RPEL 中使用时,我得到了:
?- (-8) =>> (-4).
true ;
false.
%^^^^ note here
?- 7 =>> 3.
false.
?- 40 =>> 20.
true ;
false.
%^^^^ note here
?- 20 =>> 40.
true.
Run Code Online (Sandbox Code Playgroud)
问题是什么?我该如何解决?
来自C++ Primer 5 Edition的练习使我陷入困境,这就像
练习12.3:这个类是否需要const版本的push_back和 pop_back?如果是这样,请添加它们.如果没有,他们为什么不需要?(第458页)
以下是课程.成员的定义front和back省略以简化代码.
class StrBlob
{
public:
typedef std::vector<std::string>::size_type size_type;
StrBlob();
StrBlob(std::initializer_list<std::string> il);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
// add and remove elements
void push_back(const std::string &t) {data->push_back(t);}
void pop_back();
// element access
std::string& front();
std::string& back();
private:
std::shared_ptr<std::vector<std::string>> data;
// throws msg if data[i] isn't valid
void check(size_type i, const std::string &msg) const;
};
StrBlob::StrBlob(): data(make_shared<vector<string>>()) { …Run Code Online (Sandbox Code Playgroud) 这是C++ Primer第5版的练习:
练习14.14:为什么你认为定义operator +调用operator + =而不是相反的方式更有效?(P.561)
鉴于为实现operator+=和operator+:
Sales_data&
Sales_data::operator+=(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
Sales_data
operator+(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs; // copy data members from lhs into sum
sum += rhs; // add rhs into sum
return sum;
}
Run Code Online (Sandbox Code Playgroud)
在本节末尾(14.3),作者给出了一个提示
定义算术运算符和相关复合赋值的类通常应该通过使用复合赋值来实现算术运算符.
任何人都可以使用事实/例子解释这个提示吗?
这是来自C++ Primer 5th的练习:
Run Code Online (Sandbox Code Playgroud)Exercise 4.33: Explain what the following expression does(Page158): someValue ? ++x, ++y : --x, --y
代码:
bool someVlaue = 1;
int x = 0;
int y = 0;
someVlaue ? ++x, ++y : --x,--y;
std::cout << x << std::endl << y << std::endl;
Run Code Online (Sandbox Code Playgroud)
我试过Gcc4.81和Clang3.5,都递给我:
1
0
Press <RETURN> to close this window...
Run Code Online (Sandbox Code Playgroud)
为什么不1和1?谁能解释它是如何被解释的?