下面两个代码片段之间有什么区别(如果有的话)?
来自Scala编程的Ch7的示例
def grep(pattern: String) =
for (
file <- filesHere
if file.getName.endsWith(".scala");
line <- fileLines(file)
if line.trim.matches(pattern)
) println(file + ": " + line.trim)
Run Code Online (Sandbox Code Playgroud)
还有这个
def grep2(pattern: String) =
for (
file <- filesHere
if file.getName.endsWith(".scala")
) for (
line <- fileLines(file)
if line.trim.matches(pattern)
) println(file + ": " + line.trim)
Run Code Online (Sandbox Code Playgroud)
要么
for (i <- 1 to 2)
for (j <- 1 to 2)
println(i, j)
Run Code Online (Sandbox Code Playgroud)
和
for (
i <- 1 to 2;
j <- 1 to 2 …Run Code Online (Sandbox Code Playgroud) 这是MCVE:
#include <iostream>
#include <regex>
std::string s()
{
return "test";
}
int main()
{
static const std::regex regex(R"(\w)");
std::smatch smatch;
if (std::regex_search(s(), smatch, regex)) {
std::cout << smatch[0] << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译很好:
$ clang ++ -std = c ++ 11 main.cpp
但不是:
$ clang ++ -std = c ++ 14 main.cpp
后一种情况下的错误消息(使用-std = c ++ 14):
main.cpp:14:9: error: call to deleted function 'regex_search'
if (std::regex_search(s(), smatch, regex)) {
^~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:5998:1: note:
candidate function [with _ST = std::__1::char_traits<char>, …Run Code Online (Sandbox Code Playgroud) 运行从我的宠物项目中提取的以下MWE并使用GCC 4.9.1编译(以及4.8.1)
#include <iostream>
#include <string>
#include <sstream>
class InputStringStream
{
public:
InputStringStream(const std::string& str) : istringstream(str), currentLine() {}
std::string readLine()
{
std::getline(istringstream, currentLine);
return currentLine;
}
private:
std::istringstream istringstream;
std::string currentLine;
};
int main()
{
std::string s = std::string("line1\nline2\nline3");
InputStringStream stream(s);
std::cout << stream.readLine() + "\n" + stream.readLine() + "\n" + stream.readLine() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产生以下输出
line3
line2
line1
Run Code Online (Sandbox Code Playgroud)
虽然我期待
line1
line2
line3
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
PS使用Apple LLVM编译器版本5.1编译的相同代码产生了我的期望.Visual C++ 2012在GCC方面.
如果我理解正确,我们至少有两种不同的方法来实现组合.(为了简单起见,使用智能指针实现的情况除外.我几乎不使用STL而且不想学习它.)
我们来看看维基百科的例子:
class Car
{
private:
Carburetor* itsCarb;
public:
Car() {itsCarb=new Carburetor();}
virtual ~Car() {delete itsCarb;}
};
Run Code Online (Sandbox Code Playgroud)
所以,这是一种方式 - 我们有一个指向对象的指针作为私有成员.可以将其重写为如下所示:
class Car
{
private:
Carburetor itsCarb;
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们将对象本身作为私有成员.(顺便说一句,从术语的角度来看,我是否正确地称这个实体为对象?)
在第二种情况下,隐式调用默认构造函数(如果需要调用非默认构造函数,可以在初始化列表中执行)和析构函数,则不是必须的.但这不是一个大问题......
当然,在某些方面,这两种情况的区别更为明显.例如,在第二种情况下禁止从Car类的const方法调用Carburetor实例的非const方法...
是否有任何"规则"来决定使用哪一个?我错过了什么吗?
我的我使用 Eclipse Mars 和 MSYS2。Eclipse 无法识别我的 MSYS2 安装。它包含用于 32 位编译的 Mingw-w64。我在互联网上找到的东西没有用。我该怎么办?
(void)var;对于类似于C/C++ 中的技巧的特定变量,是否有任何可移植的方法可以在 Fortran 中抑制“未使用的虚拟参数”警告?
一个激励性的例子(应弗拉基米尔·F 的要求)。策略模式,GoF示例,具有不同的断行策略,省略不必要的细节。
module linebreaking
type, abstract :: linebreaking_compositor
contains
procedure(linebreaking_compositor_compose), deferred, pass(this) :: compose
end type
abstract interface
subroutine linebreaking_compositor_compose(this)
import linebreaking_compositor
class(linebreaking_compositor), intent(in) :: this
end subroutine linebreaking_compositor_compose
end interface
type, extends(linebreaking_compositor) :: linebreaking_simple_compositor
contains
procedure, pass(this) :: compose => linebreaking_simple_compositor_compose
end type linebreaking_simple_compositor
type, extends(linebreaking_compositor) :: linebreaking_tex_compositor
contains
procedure, pass(this) :: compose => linebreaking_tex_compositor_compose
end type linebreaking_tex_compositor
type, extends(linebreaking_compositor) :: linebreaking_array_compositor
private
integer :: interval
contains
procedure, pass(this) :: compose => linebreaking_array_compositor_compose
end …Run Code Online (Sandbox Code Playgroud) 标题很好地描述了我的问题.
令人讨厌的代码行:
connect(table, SIGNAL(cellChanged(row, 5)), this, SLOT(updateSP()));
Run Code Online (Sandbox Code Playgroud)
我无法想到这个信号无效的原因.我google了一下,发现有几个人有同样的问题,但那里提出的解决方案不起作用.
我在Ubuntu Karmic上使用Qt 4.5.2,g ++.
谁知道我做错了什么?Trolltech关于cellChanged()的文档没有提到任何特殊要求.
我不知所措.
谢谢你的建议!
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
this->setupUi(this);
this->setupActions();
this->setWindowTitle(tr("CuteEdit"));
label = new QLabel(tr("No Open Files"));
this->setCentralWidget(label);
label->setAlignment(Qt::AlignCenter);
}
Run Code Online (Sandbox Code Playgroud)
通过上面的代码,我得到了一个这样的GUI(它是整个屏幕的截图,只观察电子书页面中间显示的窗口).(我使用过QT Designer)
现在,我希望用户选择文件 - >打开..出现一个对话框,文件被选中..它的内容将显示在*textEdit小部件中..功能就在下面..
void MainWindow::loadFile()
{
QString filename = QFileDialog::getOpenFileName(this);
QFile file(filename);
if (file.open(QIODevice::ReadOnly|QIODevice::Text))
{
label->hide();
textEdit->setPlainText(file.readAll());
mFilePath = filename;
QMainWindow::statusBar()->showMessage(tr("File successfully loaded."), 3000);
}
}
Run Code Online (Sandbox Code Playgroud)
窗口在行崩溃: -
textEdit-> setPlainText(file.readAll());
但如果我评论这条线: -
这 - > setCentralWidget(标签);
我的意思是我删除标签作为中央小部件,程序按预期运行..为什么?
而且,我不清楚CentralWidget的概念.请指导.
我正在尝试编写一个使用绿色函数来解决热方程的fortran程序.我使用的是fortran 90而不是77,部分原因是因为我的印象是它几乎是fortran 77的复制品,但有一些非常有用的功能.虽然主要原因是自由格式编码.我在一个名为"有用"的模块中有一些"有用的"常量和变量.我想在我的大多数程序,子程序,函数等中包含这些变量和常量.我只是在学习fortran.我已经在perl,C++,java,matlab和mathematica中编写过程序.我觉得很难.我不明白程序和子程序之间的区别.我当然不清楚模块是什么.过去12个小时我一直在研究这些术语,但尚未得到简明扼要的区别和定义.我得到了各种各样的样本,展示了如何声明这些东西,但实际上很少描述它们应该用于什么.
我真的很感激为什么我的函数"x"不能"使用"我的"有用"模块的解释.
此外,澄清前面提到的fortran功能将非常有用.
module useful
integer, parameter :: N=2
double precision, parameter :: xmin=1, xmax=10, pi=3.1415926535898
double complex :: green(N,N), solution(N), k=(2.0,0.0)
end module useful
program main
use useful
!real*8 :: delta = 2**-7
do n1 = 1, N
do n2 = 1, N
green(n1,n2) = exp((0,1)*k*abs(x(n2)-x(n1)))/(4*pi*abs(x(n2)-x(n1)))
print *, x(n2)
end do
end do
end program main
function x(n1)
use useful
real :: n1, x
x=n1*(xmax-xmin)/N
end function x
Run Code Online (Sandbox Code Playgroud)