在阅读Alexander Stepanov 的书(笔记编程)和正在进行的视频讲座时,我学到了一些关于良好界面对程序影响的惊人事实.
他解释了界面设计是非常重要的部分,它对程序有重大影响.在使用以下STL算法/函数"std :: find_if"设计接口时,他提到了以下几点.
template<class I, class P>
I find_if (I first, I last, P pred) {
while (first!=last) {
if (pred(*first)) return first;
++first;
}
return last;
}
Run Code Online (Sandbox Code Playgroud)
在上面,他解释说
所以我在分析C++ STL接口后学到的主要思想是:
基于以上几点,他提到std :: copy_n接口不正确,应该在不久的将来修复
从
template< class InputIt, class Size, class OutputIt >
OutputIt copy_n( InputIt first, Size count, OutputIt result );
Run Code Online (Sandbox Code Playgroud)
至
template< class InputIt, class Size, class OutputIt > …Run Code Online (Sandbox Code Playgroud) 这是关于在某种类中包装异常处理逻辑.在编写c ++代码时,很多时候我们需要捕获许多类型/变体的异常,具体取决于客户端抛出的内容.这导致我们在catch()子句中编写类似类型的代码(多次).
在下面的示例示例中,我编写了函数(),它可以以多种可能的形式抛出异常.
我想知道是否有可能以类的形式编写/包装这样的逻辑,以便最终用户必须一次编写类似类型的代码?它有意义还是有意义?
#include<vector>
#include<string>
#include<exception>
#include<iostream>
// this function can throw std::exception, std::string, int or unhandled
void function() {
std::vector<int> x{1,2,3,4,5};
auto val = x.at(x.size()); //throw out-of-range error
}
int main() {
try { function(); }
catch(std::exception& e) { std::cout<<e.what()<<std::endl; }
catch(std::string& msg) { std::cout<<msg<<std::endl; }
catch(int i) { std::cout<<i<<std::endl; }
catch(...) { std::cout<<"Unhandled Exception"<<std::endl; }
return 0;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我以这种方式思考,下面是伪逻辑.
Run Code Online (Sandbox Code Playgroud)class exceptionwrapper{ exceptionwrapper(function pointer* fp) { // functions which would be executing inside try } …
我无法在C++中使用VS 2012中的统一初始化程序.
可能的原因是什么?在C++程序中使用统一初始化器功能有什么问题吗?
我知道valgrind可以调用memcheck来进行内存泄漏检查,这种情况下编译出来的C++可执行程序必须包含调试信息。那么,如果我想使用 valgrind/callgrind 来执行分析,可执行文件是否必须包含调试信息?我运行了一个小测试,似乎 valgrind/callgrind 可以在没有调试信息的情况下发布可执行程序。有人可以确认吗?
gcc4 .9支持编译器警告/错误消息的着色诊断.
我们可以使用选项" fdiagnostics-color" 为特定程序启用它.目前我正在使用gcc4.9.1,我在makefile中附加了这个特殊选项,如下所示:
CC = /home/mantosh/gcc-4.9.1/bin/g++ -std=c++1y -Wall -pthread
DFLAG = -g -gdwarf-2 -fdiagnostics-color=always
OUTFILE = test
$(OUTFILE): test.cpp
$(CC) $(DFLAG) -o $(OUTFILE) test.cpp
clean:
rm -f *.o $(OUTFILE)
Run Code Online (Sandbox Code Playgroud)
如果我编译*.cpp文件,请获取以下漂亮的彩色消息.这是GCC增加的非常棒的功能.

在阅读GCC官方链接时,似乎可以使用GCC环境变量"GCC_COLORS"永久启用此设置.
有人可以解释如何设置/更改/自定义这个特定的环境变量吗?
我使用的是Ubuntu12.04/GCC4.9.1.
#include<iostream>
struct a{
int x;
};
int foo() {
a oa;
return oa.x;
}
int bar() {
int a;
return a;
}
int main() {
auto val = foo();
std::cout<<val<<"\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我们编译并运行上面的代码,sample.cpp我们得到以下结果:
$g++ -Wall -std=c++11 sample.cpp -o sample
sample.cpp: In function ‘int bar()’:
sample.cpp:13:9: warning: ‘a’ is used uninitialized in this
function [-Wuninitialized]
return a;
^
$ ./sample
-1643562384
$ ./sample
991591024
Run Code Online (Sandbox Code Playgroud)
对于上述程序,编译器会发出有关变量内部函数的未初始化使用的警告.但是,当函数尝试使用类型对象的变量时,编译器不会发出警告.abar()foo()xoastruct a
我知道这个c++11 universal initialization …
我有一个指针,它等于另一个指针
我想检查我的指针是否等于非空的指针.
int* ptr0 = new int(5);
int* ptr1 = ptr0;
delete ptr0;
if ( ?? )
{
std::cout << "ptr1 equals to a null ptr" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我该怎么写这个条件?
知道:
GNU / Linux机器上有脚本命令,该脚本命令可将所有命令行活动捕获到文件中。这是非常有用的工具,尤其是当我们学习新知识并且想要保存命令及其输出以供将来参考时。
我目前正在Windows PowerShell终端上学习Git ,我想将所有命令及其输出捕获到文件中,以备将来参考。
有什么方法/命令可以在Windows PowerShell上实现吗?
在工作的时候,我遇到了一段奇怪/令人困惑的代码,我觉得这些代码与匿名 对象生命周期概念有关.以下是示例代码:
#include<iostream>
#include<string>
class A {
private:
int i;
std::string s;
public:
A(int ii, std::string ss = "Hello") { i = ii; s = ss; }
void Display() { std::cout<<i<<"\n"; }
~A() { std::cout<<"A::~A()"<<"\n";}
};
void function()
{
A a = 1;
//A a = A(1);
a.Display();
}
int main()
{
function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
VS2010中的输出1(如果A a = 1)
1
A::~A()
Run Code Online (Sandbox Code Playgroud)
VS2010中的输出2(如果A a = A(1))
A::~A()
1
A::~A()
Run Code Online (Sandbox Code Playgroud)
当析构函数被调用两次(包括匿名)对象时,output2完全有意义.
但是输出1让我困惑,无法理解为什么析构函数 …
我使用的SO帖子中提到的步骤安装gcc4.9 这里.我正在使用C++ 14中引入的最新功能std :: exchange()实用程序功能.
#include<list>
#include<utility>
int main() {
std::list<int> lin{5,6,7,8,9};
auto lout = std::exchange(lin, {1,2,3,4});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我执行了以下步骤来编译上面的示例程序,并得到以下编译错误.一段时间后,我意识到(由于没有编译器消息的警告/提示)这个功能已经添加到C++ 14标准中,所以我需要在-std=c++1y这里使用.
$g++ -std=c++11 main.cpp
main.cpp: In function ‘int main()’:
main.cpp:5:14: error: ‘exchange’ is not a member of ‘std’
auto lout = std::exchange(lin, {1,2,3,4});
^
Run Code Online (Sandbox Code Playgroud)
如果我们使用C++ 11标准功能但没有提供-std=c++11,那么GCC会发出警告消息/暗示您的程序正在使用C++ 11中引入的功能,如下所示:
main.cpp:4:21: warning: extended initializer lists only available with
-std=c++11 or -std=gnu++11
std::list<int> lin{5,6,7,8,9};
Run Code Online (Sandbox Code Playgroud)
此消息很棒,让用户可以区分实际的编译错误消息而不包括-std=c++11选项.
但是在使用gcc4.9 for C++ 1y功能时-std=c++11,没有这样的警告消息/提示?我想知道可能的原因是什么?
我认为 pthread_join 应该总是返回一个值,然后允许主线程在此之后处理代码。根据我过去的经验,这会奏效。但现在我被它困住了。不知何故,它只是不返回并阻塞主线程。或者它可能是执行任务的主线程。我不知道为什么。在下面的代码中,在终止客户端之前,我无法访问“Thread created2”。任何的想法?
int main(int argc, char *argv[]) {
int sockfd, port; /* listen on sock_fd, new connection on new_fd */
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* connector's address information */
socklen_t sin_size;
if(signal(SIGINT, sigintEvent) == SIG_ERR)
printf("can't catch SIGINT!");
/* generate the socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (argc > 1) {
port = atoi(argv[1]);
} else {
port = MYPORT;
}
/* …Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
void arrSelectSort(int *[], int), showArrPtr(int *, int);
void showArray(int [] , int);
int main()
{
int numDonations;
int *arrPtr;
cout << "What was the number of donations?: ";
cin >> numDonations;
arrPtr = new int[numDonations];
cout << "What were the donations values?: ";
for (int count = 0; count < numDonations; count++)
cin >> arrPtr[count];
arrSelectSort(arrPtr, 3);
cout << "The donations, sorted in ascending order are: \n";
showArrPtr(arrPtr, 3);
cout << "The donations, in their …Run Code Online (Sandbox Code Playgroud)