C++ C+中的容器初始化+;98

C++ C+中的容器初始化+;98,c++,stl,c++98,C++,Stl,C++98,我必须按照以下规则构造一个有序容器(它必须是iterable): 如果条件为true,则容器为{1,0},否则为{0,1} 我有以下代码,但我不觉得它“优雅”: 向量有序边; 如果(条件) { 有序侧。推回(1); 有序边。推回(0); } 其他的 { 有序边。推回(0); 有序侧。推回(1); } 有没有更好的方法(从简洁性和性能的角度)来做到这一点 您可以实现如下内容: vector<int> orderedSides(2, 0); (condition ? orderedSi

我必须按照以下规则构造一个有序容器(它必须是iterable):

如果条件为true,则容器为
{1,0}
,否则为
{0,1}

我有以下代码,但我不觉得它“优雅”:

向量有序边;
如果(条件)
{
有序侧。推回(1);
有序边。推回(0);
}
其他的
{
有序边。推回(0);
有序侧。推回(1);
}

有没有更好的方法(从简洁性和性能的角度)来做到这一点

您可以实现如下内容:

vector<int> orderedSides(2, 0);
(condition ? orderedSides.front() : orderedSides.back()) = 1;
向量有序边;
有序侧。推回(条件?1:0);
订购侧。推回(条件?0:1);
我不认为它更有表现力,但我觉得它更优雅

orderedSides.push_back(0);
orderedSides.push_back(1);
if (condition)
  std::iter_swap(orderedSides.begin(), orderedSides.begin()+1);

我知道这要花很多钱。作为候选者之一。

你可以在效率和避免重复之间折衷,用条件初始化第一个,从第一个开始初始化第二个

vector<int> orderedSides(1, bool(condition)) ;
orderedSides.push_back(!orderedSides.back());
向量有序边(1,布尔(条件)); orderedSides.push_back(!orderedSides.back());
即使在C++98中,也可以从数组中填充
std::vector

下面是一个例子:

#include <iostream>
#include <vector>

int main() {
    bool condition = false;
    std::cout << "condition is: " << std::boolalpha << condition << '\n';

    int arr[][2] = {{0,1}, {1,0}};
    int index = condition;
    std::vector<int> v(arr[index], arr[index]+2);

    for (int i = 0; i < v.size(); i++)
        std::cout << v[i] << ' ';
    std::cout << '\n';
}
供参考:

如果构建元素(您问题中的
int
s,无论在现实生活中是什么)是免费的且副作用较小:

static const int data[] = { 0, 1, 0 };
std::vector<int> orderedSides (data+condition, data+condition+2);

您可以在实施reserve之前致电reserve。最后,STD::初始化函数列表构造函数从性能的角度来看不是更好,因为它们必须将数据复制到容器中。在C++ 11之前,基本上是唯一的方法。@ Someprogrammerdude C++ C++ NoBOS:在C++ 11中,如何做这一点?@ RealBooW<代码> vector OrrdEdgs= {条件,条件}} /代码>。Iff
condition
是一个
bool
表达式。如果
condition
可以转换为
int
(当然也可以是
int
),但结果可能不是
1
0
,那么
{!!condition,!condition}
@Someprogrammerdude哇,太酷了。谢谢这将是
std::swap
,因为
front
back
返回引用而不是迭代器因为第二行更短,对优化器的依赖更少。@重复数据消除器,准确地说,完美的注释!我发现这段代码更难阅读,其意图也很不清楚。我也不太喜欢这部电影的演员阵容,但那只是我的看法。OP应该避开这一点。没有否决票,因为OP确实说过简洁是最重要的,甚至可能是代码高尔夫?OP也提到了性能,这看起来性能很差。您有一个与输入相关的地址,该地址始终为前两个存储中的一个别名。这是OOO CPU不喜欢的事情@一些程序员的回答(在评论中,boo!)看起来更好,因为它有两个绝对不相关的存储。如果条件为真,数组应该按降序排列。也许
[!condition]
使用不同的名称会读得更好,比如
[!descending]
={bool(condition)}
不是要求的C++98。在典型的C++03标准库实现中,该构造函数是如何实现的?它只是在内部对数组执行
循环吗?@CodyGray我希望实现首先调整大小/保留足够的内存。但是,我认为写一个循环、memcpy或std::copy来实现这一点并没有多大区别。
vector<int> orderedSides(1, bool(condition)) ;
orderedSides.push_back(!orderedSides.back());
#include <iostream>
#include <vector>

int main() {
    bool condition = false;
    std::cout << "condition is: " << std::boolalpha << condition << '\n';

    int arr[][2] = {{0,1}, {1,0}};
    int index = condition;
    std::vector<int> v(arr[index], arr[index]+2);

    for (int i = 0; i < v.size(); i++)
        std::cout << v[i] << ' ';
    std::cout << '\n';
}
$ g++ tt.cc && ./a.out
condition is: false
0 1 
static const int data[] = { 0, 1, 0 };
std::vector<int> orderedSides (data+condition, data+condition+2);
#include <iostream>
#include <vector>

std::vector<int> make(bool cond)
{
    static const int data[] = { 0, 1, 0 };
    return std::vector<int> (data+cond, data+cond+2);
}

std::ostream& operator<<(std::ostream& os, const std::vector<int>& v)
{
    return os << "{ " << v[0] << ", " << v[1] << " }";
}    

int main()
{
    std::cout << "true:  " << make(true) << "\n"
              << "false: " << make(false) << "\n";
}
true:  { 1, 0 }
false: { 0, 1 }