小编Ant*_*ron的帖子

如何使用std :: regex匹配多个结果

例如.如果我有一个像"第一个第二个第三个"的字符串,我想在一个操作中匹配每个单词,逐个输出.

我只是认为"(\ 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)

我期待着你的帮助.:)

c++ regex

19
推荐指数
6
解决办法
3万
查看次数

使用成员函数作为构造函数未定义行为的参数?

#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.

它在调用构造函数之前调用成员函数.标准是否定义了行为?

c++ undefined-behavior

19
推荐指数
1
解决办法
1154
查看次数

在swift中调用超级指定初始化程序之前初始化属性值的目的是什么?

这是我的代码:

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'和超类的初始化顺序是否真的无关紧要.

swift

6
推荐指数
1
解决办法
328
查看次数

泛型类型的初始化器不会在swift中继承吗?

这是我的代码:

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)

是否应该继承要求类型指定的泛型类型的初始值设定项? …

generics initializer swift

6
推荐指数
1
解决办法
519
查看次数

覆盖swift中的泛型函数错误

这是代码:

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)

generics overriding swift

6
推荐指数
2
解决办法
1871
查看次数

如何在c ++ 11中实现CAS

我想知道如何在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)

有没有更好的方法来实现这个?

c++ c++11

5
推荐指数
1
解决办法
795
查看次数

如何在 swift 中实现自定义运算符 []

我用 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)

custom-operator swift

3
推荐指数
1
解决办法
1160
查看次数

如何在C++11代码中实现单例模式和工厂模式

我正在尝试同时实现单例模式和工厂模式。在我看来,每种工厂只能有一个工厂,它们基于一个抽象类,并使用工厂来生产相应的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)

c++ singleton design-patterns

1
推荐指数
1
解决办法
6082
查看次数