小编Dug*_*las的帖子

将两个查询合并为一个

有没有办法将这两个查询合并为一个?

query = "select foo from TABLE where foo like '%foo%'";

if (query.empty())
    query = "select bar from TABLE where bar like '%foo%'"
Run Code Online (Sandbox Code Playgroud)

更新:

select ifnull(foo,bar) from TABLE where foo like 'foo%' or bar like '%foo%';
Run Code Online (Sandbox Code Playgroud)

感谢Kamal的想法

mysql sql join

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

C++ Decorator模式

我有一个向量和几个类(位于不同的文件中)来修改它.
我希望全局访问std::vector,但只有在派生类中,当每个调用存储前一个结果时,最后一个对象应该返回总结果

你能解释一下如何使用Decorator模式构建高性能接口std::vector吗?
我可能错了,可能需要其他模式.

// A.h
class A () {
      public : 
           vector<int> set(vector<int> &vec);

            //return total result
           vector<int> get() {
               return vector;
           }
};

// B.h
class B () {
    //add new elements into vector
    //for example, add 2,3,4
};

// C.h
class C () {
    //remove some elements from vector
    //for example, remove last element
};

//...

// main.cpp
#include "A.h"
#include "B.h"
#include "C.h"

int main () {

    vector<int> set;
    set.push_back(1); …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance design-patterns stl decorator

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

海湾合作委员会警告[Wuninitialized]

为什么GCC 4.7在函数内实例化一个类(带指针)时会抱怨?

坏:

#include "foo.h"

int fn () {
    Foo *foo;
    foo->method();

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

main.cpp:在成员函数'int foo()'中:main.cpp:21:52:警告:'fn'可以在此函数中未初始化使用[-Wuninitialized]

好:

#include "foo.h"

Foo *foo;

int fn () {
    foo->method();

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

好:

#include "foo.h"

int fn () {
    Foo foo;
    foo.method();

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ gcc gcc-warning

5
推荐指数
2
解决办法
7996
查看次数

替换向量中的值

我想添加字符串开头的右括号 [[

我尝试过使用find_if,replace_if但事实证明有些事情是不对的

std::vector<std::string> vector(3);
Run Code Online (Sandbox Code Playgroud)

包含:

    0: text
    1: [[text
    2: text
Run Code Online (Sandbox Code Playgroud)

我想要的是:

    0: text
    1: [[text]]
    2: text
Run Code Online (Sandbox Code Playgroud)

你能帮忙解算算法吗?

c++ stl

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

替换像replaceAll这样的字符(字符串,开头,结尾,'_',' - ')

我想替换字符串text=s_o_m_e=texttext=s-o-m-e=text

我有一个起始和结束索引:

std::string str("text=s_o_m_e=text");

std::string::size_type start = str.find("text="), end;

if (start != std::string::npos) {
    end = str.find("=", start);

    if (end != std::string::npos) {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,我正在寻找这样的函数:

replaceAll(string, start, end, '_', '-');
Run Code Online (Sandbox Code Playgroud)

UP:

std::replace(str.begin() + start, str.begin() + end, '_', '-');
Run Code Online (Sandbox Code Playgroud)

谢谢,Blastfurnace

c++ replace stl

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

标签 统计

c++ ×4

stl ×3

decorator ×1

design-patterns ×1

gcc ×1

gcc-warning ×1

inheritance ×1

join ×1

mysql ×1

replace ×1

sql ×1