我正在阅读文本文件中的行,我想知道这是否是一个好方法?我必须编写函数numberoflines
来减少number_of_lines variable
一个,因为在while循环中,对于它读取的每一行,它都会向number_of_lines变量添加2.
#include <iostream>
#include <fstream>
using namespace std;
int number_of_lines = 0;
void numberoflines();
int main(){
string line;
ifstream myfile("textexample.txt");
if(myfile.is_open()){
while(!myfile.eof()){
getline(myfile,line);
cout<< line << endl;
number_of_lines++;
}
myfile.close();
}
numberoflines();
}
void numberoflines(){
number_of_lines--;
cout<<"number of lines in text file: " << number_of_lines << endl;
}
Run Code Online (Sandbox Code Playgroud)
还有其他更容易更好的方法吗?
可能重复:
从函数返回unique_ptr
20.7.1.2 [unique.ptr.single]定义了这样的复制构造函数:
// disable copy from lvalue
unique_ptr(const unique_ptr&) = delete;
unique_ptr& operator=(const unique_ptr&) = delete;
Run Code Online (Sandbox Code Playgroud)
那么,为什么下面的代码编译得很好?
#include <memory>
#include <iostream>
std::unique_ptr< int > bar()
{
std::unique_ptr< int > p( new int(4));
return p;
}
int main()
{
auto p = bar();
std::cout<<*p<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我编译它像这样:
g++ -O3 -Wall -Wextra -pedantic -std=c++0x kel.cpp
Run Code Online (Sandbox Code Playgroud)
编译器:g ++版本4.6.1 20110908(Red Hat 4.6.1-9)
我喜欢C#3.0特性,特别是lambda表达式,自动实现的属性,或者在适当的情况下也隐式输入局部变量(var
关键字),但当我的老板透露我正在使用它们时,他让我不要在工作中使用任何C#3.0功能.有人告诉我,对于大多数开发人员而言,这些功能并不标准且令人困惑,其实用性值得怀疑.我被限制只使用C#2.0功能,他也在考虑禁止匿名方法.
由于我们的目标是.NET Framework 3.5,因此我看不出这些限制的任何原因.在我看来,也许唯一的缺点是我的几个同事和老板(也是程序员)必须学习一些C#3.0的基础知识,这应该不难.你怎么看待这件事?我的老板是对的,我错过了什么吗?在C#是主要编程语言的开发公司中,有没有充分的理由限制这种限制?
我读的意见终极版约在一周的香草萨特的大师virtual
功能,终于看见他提这一点:
[...]"最终的使用是罕见的" - 嗯,他们有点.我不知道很多,在标准化过程中,Bjarne反复询问它解决的问题和应该使用的模式的例子,我不记得任何突出的主要问题.我唯一知道的是,如果你正在定义一个库模块(它还不是标准概念),那么使叶子类最终可以为编译器提供更多信息来虚拟化调用,因为知道库外的代码"进一步推导出来,但我不确定这些日子在整个计划优化(包括积极的虚拟化)的存在下有多重要.
这个答案没有提供很多final
关于课程用例的例子,我很想知道它可以解决哪些问题.你知道吗,或者final
课堂上只会变成一些模糊不清且几乎未使用过的功能?
示例波纹管编译,但输出相当奇怪:
#include <iostream>
#include <cstring>
struct A
{
int a;
char b;
bool c;
};
int main()
{
A v;
std::memset( &v, 0xff, sizeof(v) );
std::cout << std::boolalpha << ( true == v.c ) << std::endl;
std::cout << std::boolalpha << ( false == v.c ) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
true
true
Run Code Online (Sandbox Code Playgroud)
有人能解释为什么吗?
如果重要,我使用的是g ++ 4.3.0
如果我为迭代器使用默认构造函数,如何检查它是否稍后分配?
对于指针,我可以这样做:
int *p = NULL;
/// some code
if ( NULL == p ) {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
如何为迭代器执行上述操作?有可能吗?
#include <iostream>
#include <list>
int main ()
{
std::list<int>::iterator it;
if ( NULL == it ) // this fails
{
std::cout<<"do stuff" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道为什么隐式类型转换不适用于类模板上的外部运算符重载.这是工作的非模板化版本:
class foo
{
public:
foo() = default;
foo(int that)
{}
foo& operator +=(foo rhs)
{
return *this;
}
};
foo operator +(foo lhs, foo rhs)
{
lhs += rhs;
return lhs;
}
Run Code Online (Sandbox Code Playgroud)
正如所料,以下行正确编译:
foo f, g;
f = f + g; // OK
f += 5; // OK
f = f + 5; // OK
f = 5 + f; // OK
Run Code Online (Sandbox Code Playgroud)
另一方面,当类foo
声明为这样的简单模板时:
template< typename T >
class foo
{
public:
foo() = default;
foo(int that)
{} …
Run Code Online (Sandbox Code Playgroud) 我在bash脚本中运行valgrind,并将valgrind的输出定向到一个文件.像这样 :
valgrind --leak-check=full --show-reachable=yes --xml=yes --xml-file=unit_tests_valgrind.out.xml ./unit_tests_runner
Run Code Online (Sandbox Code Playgroud)
本RET_VALUE=$?
打算把程序(以上unit_tests_runner)的返回值,而不是从Valgrind的.检查valgrind是否发现内存问题的唯一方法是打开日志.
有没有办法检查脚本是否有内存问题?如果有,怎么样?
对于我的Qt项目,我使用.pro文件,该文件包含一个单独的.pri文件,用于各种标头,源,表单和资源文件.但是,每次添加新文件时,我都需要手动将其添加到.pri文件中.这很乏味且容易出错.有没有办法"神奇地"添加目录中的所有文件,直接在.pri文件中或通过告诉qmake预先运行单独的脚本?
在c ++ 0x中,shared_ptr将从tr1移动到std.那么要包括哪个标题来获取它?
我正在使用g ++ 4.5(ubuntu 10.10)