例如.如果我有一个像"第一个第二个第三个"的字符串,我想在一个操作中匹配每个单词,逐个输出.
我只是认为"(\ b\S*\b){0,}"会起作用.但实际上并没有.
我该怎么办?
这是我的代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
regex exp("(\\b\\S*\\b)");
smatch res;
string str = "first second third forth";
regex_search(str, res, exp);
cout << res[0] <<" "<<res[1]<<" "<<res[2]<<" "<<res[3]<< endl;
}
Run Code Online (Sandbox Code Playgroud)
我期待着你的帮助.:)
#include <cstdio>
class A
{
public:
A(int){puts("3");};
int foo(){puts("4");return 10;}
};
int main()
{
A a(a.foo());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出4和3.
它在调用构造函数之前调用成员函数.标准是否定义了行为?
这是我的代码:
class Base
{
init(){
print("Super!")
}
}
class Test : Base
{
internal var y:Int
convenience init(_ a:Int)
{
self.init()
print("\(a)")
}
override init()
{
super.init() //Error!!! Property 'self.y' not initialized at super.init call
y = 123
}
}
Run Code Online (Sandbox Code Playgroud)
我认为应该编译:
y在'Base'类中是不可见的,无论是y's'和超类的初始化顺序是否真的无关紧要.
这是我的代码:
public class A<T : Any> {
public init(n : Int) {
print("A")
}
}
public class B : A<Int> {
}
public class C : B {
}
let x = C(n: 123)
Run Code Online (Sandbox Code Playgroud)
这无法编译并大喊这样的错误:
repl.swift:9:9: error: 'C' cannot be constructed because it has no accessible initializers
Run Code Online (Sandbox Code Playgroud)
可以编译以下代码.
public class A {
public init(n : Int) {
print("A")
}
}
public class B : A {
}
public class C : B {
}
let x = C(n: 123)
Run Code Online (Sandbox Code Playgroud)
是否应该继承要求类型指定的泛型类型的初始值设定项? …
这是代码:
class Test<T> {
func foo<S:SequenceType where S.Generator.Element == T>(par : S){
print("foo")
}
}
class TestInh : Test<Int> {
override func foo<S:SequenceType where S.Generator.Element == Int>(par : S) {
print("loo")
}
}
Run Code Online (Sandbox Code Playgroud)
它大喊这样的错误:
repl.swift:8:19: error: method does not override any method from its superclass
override func foo<S:SequenceType where S.Generator.Element == Int>(par : S) {
~~~~~~~~ ^
Run Code Online (Sandbox Code Playgroud)
我怎样才能覆盖超类中的方法Test<Int>?
==================额外=======================
谈到代码打击.
class Test<T> {
func foo(par : T){
print("foo")
}
}
class TestInh : Test<Int> {
override func foo(par …Run Code Online (Sandbox Code Playgroud) 我想知道如何在c ++ 11中实现compare_and_swap.这是我试过的:
template<typename T>
T compare_and_swap(atomic<T>& reg,T newVal )
{
bool success = false;
T oldVal;
do
{
oldVal = reg.load();
success = reg.compare_exchange_weak(oldVal,newVal);
}while(!success);
return oldVal;
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来实现这个?
我用 swift 编写了一个简单的队列类。它是通过数组实现的。现在我希望它的执行更像内置数组。所以我需要实现该[]运算符但失败了。有人帮忙吗?
public class SimpleQueue<T : Any>
{
private var frontCur = 0
private var reuseCur = -1
private var capacity = 0
private var impl = [T]()
public var count : Int
{
get
{
return impl.count - frontCur
}
}
public func empty() -> Bool
{
return self.count == 0
}
public func size() -> Int
{
return impl.count
}
public func append(o : T)
{
if(frontCur > reuseCur && reuseCur >= 0)
{
impl[reuseCur] …Run Code Online (Sandbox Code Playgroud) 我正在尝试同时实现单例模式和工厂模式。在我看来,每种工厂只能有一个工厂,它们基于一个抽象类,并使用工厂来生产相应的shape对象。
但我收到一个错误:
错误LNK2001:无法解析的外部符号“私有:静态类TriangleFactory * TriangleFactory ::实例”(?instance@TriangleFactory@@0PAV1@A)
我不知道为什么会发生这种情况。你能告诉我如何解决它吗?代码如下。
#include<type_traits>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<memory>
#include<time.h>
using namespace std;
class Shape
{
public:
virtual void display();
protected:
int x, y;
};
class MyTriangle:public Shape
{
public:
MyTriangle(int _x, int _y)
{
x = _x;
y = _y;
}
MyTriangle()
{
x = rand() % 200;
y = rand() % 200;
}
void display()
{
printf("%d %d\n",x,y);
}
};
class SuperFactory
{
public:
virtual Shape* Execute() = 0;
};
class TriangleFactory :public …Run Code Online (Sandbox Code Playgroud)