我有两个类,我们称之为SomeClass和OtherClass.
SomeClass是模板化的:
template <typename T> class SomeClass
{
public:
SomeClass (const T& value);
};
Run Code Online (Sandbox Code Playgroud)
OtherClass不是模板化的,而是使用SomeClass.
class OtherClass
{
public:
OtherClass (const SomeClass& c, const std::string s);
};
Run Code Online (Sandbox Code Playgroud)
应该以这种方式调用它们:
SomeClass<int> some(5);
OtherClass other(some, "hello, world!");
other.doSomethingWithSome();
Run Code Online (Sandbox Code Playgroud)
...显然,这将无法编译,因为编译器需要知道SomeClass的类型...
对我来说不幸的是,SomeClass的类型几乎可以是任何东西(尽管使用的实际类型的数量是有限的,只是无关的),并且在开发过程中可能经常发生变化.(我知道,我知道,我想我真的可以使用SomeClass'类型并将其传递给模板化的OtherClass,但由于存在许多实例,因此它非常繁琐;而且,我想假装两个班都不知道其他人的工作.:))
问题很简单:我该如何使用这种语法?(无需模板化OtherClass.)
我正在尝试使用std :: transform填充std :: map.下一个代码编译没有错误:
std::set<std::wstring> in; // "in" is filled with data
std::map<std::wstring, unsigned> out;
std::transform(in.begin(), in.end()
, boost::counting_iterator<unsigned>(0)
, std::inserter(out, out.end())
, [] (std::wstring _str, unsigned _val) { return std::make_pair(_str, _val); }
);
Run Code Online (Sandbox Code Playgroud)
但如果我更换字符串
, [] (std::wstring _str, unsigned _val) { return std::make_pair(_str, _val); }
Run Code Online (Sandbox Code Playgroud)
同
, std::make_pair<std::wstring, unsigned>
Run Code Online (Sandbox Code Playgroud)
要么
, std::ptr_fun(std::make_pair<std::wstring, unsigned>)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
foo.cpp(327): error C2784: '_OutTy *std::transform(_InIt1,_InIt1,_InTy (&)[_InSize],_OutTy (&)[_OutSize],_Fn2)' : could not deduce template argument for '_InTy (&)[_InSize]' from 'boost::counting_iterator<Incrementable>'
with
[
Incrementable=unsigned int
]
C:\Program …
Run Code Online (Sandbox Code Playgroud) 有时我想在 vim 中将当前行与 line up 或 below 交换。我可以用命令:m+1
或:m-1
. 不过也太啰嗦了。有没有更短的方法做同样的事情?
struct Foo {};
struct Bar : Foo {};
Foo &foo = Bar(); // without const
Run Code Online (Sandbox Code Playgroud)
正如在这个问题的答案和评论中写的那样,我无法为引用分配右值.但是,我可以编译此代码(MSVC++ 2010)而不会出现错误或警告.这是我的编译器的已知问题吗?
我正在尝试在 Scala 中跟踪递归处理。以下是代码示例:
def factorial(n: Int): Int =
if (n <= 1) 1
else {
println("Computing factorial of " + n + " - I first need factorial of " + (n-1))
def result = n * factorial(n - 1)
println("Computed factorial of " + n)
result
}
println(factorial(3))
Run Code Online (Sandbox Code Playgroud)
以下是输出:
Computing factorial of 3 - I first need factorial of 2
Computed factorial of 3
Computing factorial of 2 - I first need factorial of 1
Computed factorial of 2 …
Run Code Online (Sandbox Code Playgroud) algorithm recursion functional-programming scala lazy-evaluation
我有一个 class Foo extends Bar
和一个List
或其他基类集合:
val bars: Iterable[Bar]
Run Code Online (Sandbox Code Playgroud)
我需要Foo
从集合中提取所有元素。以下是代码:
val fooes: Iterable[Foo] = bars
.filter(x => Try(x.isInstanceOf[Foo]).isSuccess))
.map(_.isInstanceOf[Foo])
Run Code Online (Sandbox Code Playgroud)
有更简洁的方法吗?
我想从Iterable[Try[Int]]
所有有效值的列表中提取( Iterable[Int]
)
val test = List(
Try(8),
Try(throw new RuntimeException("foo")),
Try(42),
Try(throw new RuntimeException("bar"))
)
Run Code Online (Sandbox Code Playgroud)
以下是从 打印所有有效值的方法test
:
for {
n <- test
p <- n
} println(p)
// Output
// 8
// 42
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试将有效值保存到列表时,我收到了一个错误:
val nums: Seq[Int] = for {
n <- list
p <- n // Type mismatch. Required: IterableOnce[Int], found Try[Int]
} yield(p)
println(nums)
Run Code Online (Sandbox Code Playgroud)
如何修复错误以及它为什么被提出?
我有一个文件夹,其中包含按时间戳命名的媒体文件,如下所示yyyyMMdd_HHmmss_*.*
。我需要将它们重命名为yyyy-MM-dd HH-mm-ss *.*
例如,我需要将文件重命名20181019_210353_BURST2.jpg
为2018-10-19 21-03-53 BURST2.jpg
有一个我的丑陋方法
PS E:> gci | Rename-Item -NewName { $_.Name.Substring(0,4) + '-' + $_.Name.Substring(4,2) + '-' + $_.Name.Substring(6,2) + ' ' + $_.Name.Substring(9,2) + '-' + $_.Name.Substring(11,2) + '-' + $_.Name.Substring(13,2) + $_.Name.Substring(15) }
Run Code Online (Sandbox Code Playgroud)
实现我的目的的正确命令是什么?
我想在MSVC2010中初始化零指针的c字符串数组
// Foo.h
#pragma once
class Foo {
int sz_;
char **arr_;
public:
Foo();
~Foo();
// ... some other functions
};
// Foo.cpp
#include "Foo.h"
#define INITIAL_SZ 20
Foo::Foo() : sz_(INITIAL_SZ) {
// there I have to initialize arr_ (dynamic array and can be enlarged later)
arr_ = (char **)calloc(INITIAL_SZ * sizeof (char *)); // ???
// or maybe arr_ = new ...
}
Run Code Online (Sandbox Code Playgroud)
如何正确初始化arr_
?我不被允许使用STL,MFC等.