例如,我不能这样写:
class A
{
vector<int> v(12, 1);
};
Run Code Online (Sandbox Code Playgroud)
我只能写这个:
class A
{
vector<int> v1{ 12, 1 };
vector<int> v2 = vector<int>(12, 1);
};
Run Code Online (Sandbox Code Playgroud)
对C++ 11语言设计的差异有何考虑?
从C++ 11开始,标准库容器和std::string构造函数都采用初始化列表.这个构造函数优先于其他构造函数(甚至,正如@ JohannesSchaub-litb在评论中指出的那样,甚至忽略了其他"最佳匹配"标准).当将所有带括号()的构造函数转换为其支撑版本时,这会导致一些众所周知的陷阱{}
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
void print(std::vector<int> const& v)
{
std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, ","));
std::cout << "\n";
}
void print(std::string const& s)
{
std::cout << s << "\n";
}
int main()
{
// well-known
print(std::vector<int>{ 11, 22 }); // 11, 22, not 11 copies of 22
print(std::vector<int>{ 11 }); // 11, not 11 copies of 0
// more surprising
print(std::string{ 65, 'C' }); // AC, not 65 …Run Code Online (Sandbox Code Playgroud) 我在这段代码中有内存损坏:
#include <string>
#include <iostream>
#include <vector>
#include <initializer_list>
int main() {
std::vector<std::initializer_list<std::string>> lists = {
{
{"text1"},
{"text2"},
{"text3"}
},
{
{"text4"},
{"text5"}
}
};
int i = 0;
std::cout << "lists.size() = " << lists.size() << std::endl;
for ( auto& list: lists ) {
std::cout << "lists[" << i << "].size() = " << lists[i].size() << std::endl;
int j = 0;
for ( auto& string: list ) {
std::cout << "lists[" << i << "][" << j << …Run Code Online (Sandbox Code Playgroud) 以下代码的正确行为是什么?
#include <vector>
#include <iostream>
int main()
{
std::vector<char> v = { "y", "z" };
std::cout << v[0];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这不是一个未定义的行为吗?
考虑初始化列表构造函数用法的这个示例:
std::vector<std::string> v = { "xyzzy", "plugh", "abracadabra" };
std::vector<std::string> v({ "xyzzy", "plugh", "abracadabra" });
std::vector<std::string> v{ "xyzzy", "plugh", "abracadabra" };
Run Code Online (Sandbox Code Playgroud)
它们之间是否有任何差异(甚至是轻微的)?
在一个大型项目中,您必须定义一个标准,您会选择哪种样式?
我更喜欢第一种风格,第三种风格很容易与使用args的构造函数调用混淆.第一种风格看起来也很熟悉其他编程语言.
问题来自于C++ Primer第5版的练习:
编写一个程序,将char*指针列表中的元素分配给C风格的字符串到一个字符串向量.
---------------- Oringinal Question ------------
首先,我尝试以下有点直接的方式:
vector<char *> vec = {"Hello", "World"};
vec[0][0] = 'h';
Run Code Online (Sandbox Code Playgroud)
但编译代码我收到警告:
temp.cpp:11:43: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
vector<char *> vec = {"Hello", "World"};
^
Run Code Online (Sandbox Code Playgroud)
运行./a.out我得到了一个
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)
我想是因为我试着写一个const char.所以我尝试另一种方式:
char s1[] = "Hello", s2[] = "World";
vector<char *> vec = {s1, s2};
vec[0][0] = 'h';
Run Code Online (Sandbox Code Playgroud)
这次没关系.但这似乎有点单调乏味.有没有其他优雅的方法来初始化字符串文字的向量?
为什么以下代码编译?它用clang和print编译并运行良好first.但是,我认为正确的行为应该是抱怨并发出正确的错误.
#include <iostream>
#include <string>
int main()
{
std::string s{ "first", "second" };
std::cout << s << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这个问题的灵感来自于此.
我有以下代码test.cpp:
#include <vector>
#include <string>
class A {
public:
static const std::vector<std::string> foo;
};
const std::vector<std::string> A::foo {{"bar", "baz"}};
int main() {}
Run Code Online (Sandbox Code Playgroud)
它编译,但当我运行它时,我收到以下错误:
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
为什么我收到此错误?
希望无关紧要:我正在使用g ++ 4.8.2 -std=c++11.
旁白:我故意foo在课外初学.如果我在类中进行,编译器会告诉我需要一个类外的初始化(这很荒谬,imo).