我正在尝试使用MacPorts安装的clang在C++ 11中初始化列表功能.编译这个简单的代码时:
#include <vector>
int main()
{
std::vector<int> a {1, 3, 5};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
stephen-chus-mac-pro:~ stephenchu$ clang -std=c++0x -c text.cxx -I/opt/local/include -v
clang version 3.1 (trunk 154872)
Target: x86_64-apple-darwin10.8.0
Thread model: posix
"/opt/local/libexec/llvm-3.1/bin/clang" -cc1 -triple x86_64-apple-macosx10.6.0 -emit-obj -mrelax-all -disable-free -main-file-name text.cxx -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 127.2 -v -coverage-file text.o -resource-dir /opt/local/libexec/llvm-3.1/bin/../lib/clang/3.1 -I /opt/local/include -fmodule-cache-path /var/folders/UL/ULMxdJJtEQuI+WuToNAFpk+++TI/-Tmp-/clang-module-cache -std=c++0x -fdeprecated-macro -fdebug-compilation-dir /Users/stephenchu -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o …Run Code Online (Sandbox Code Playgroud) 我有一个C++容器,我想运行一个循环的次数与该容器中的元素相同.但我不关心循环期间容器中的值.例如:
for (const auto& dummy : input) {
cout << '.';
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,dummy是一个未使用的变量,我已指示编译器禁止这些.
我提出的两个不优雅的解决方案是(void)dummy;在循环体中说要使编译器静音,或者使用从0到0的旧式for循环distance(begin(input), end(input)).
我尝试省略变量名但无法编译(没什么大惊喜).
我正在使用GCC 4.7.2.
作为一名C++初级程序员,我注意到无论您使用什么IDE /编译器,您都不需要显式包含stl(标准模板库).这是否意味着我可以依靠stl"始终可用"?
我的意思是,如果我想使用std::cout例如,我只包括iostreamstl 的一部分:
#include <iostream>
Run Code Online (Sandbox Code Playgroud)
......并且不需要像#include <std>以前那样继续做类似的事情:
std::cout << "Hello world!" << std::endl;
Run Code Online (Sandbox Code Playgroud)
此外:我可以依靠stl的一致性吗?stl的每个函数/方法是否总是以相同的方式运行?或者C++版本,操作系统或编译器之间是否有任何变化?
我问这个问题,因为当你不知道某些陷阱时,其他图书馆有时真的很痛苦.例如,Eigen(对于线性代数的东西)对我来说真的很难实现,我注意到一些版本之间改变行为......
我正在阅读关于C++的教程,并且出现了以下内容.没有提供其他细节进一步解释
C++是一种"语言联盟",支持多范式编程,我们有很多选择.
当C++被称为语言联合以及什么是多范式编程时,它意味着什么?
我有一个函数将一个向量复制到另一个向量(以及许多其他东西)。代码的简化版本如下。
void Fun(std::vector<double> &in, std::vector<double> &out) {
out = in;
}
Run Code Online (Sandbox Code Playgroud)
我关心最大化效率,因为该函数将运行多次。因此,我想尽可能避免重新分配内存。向量“in”可能已经为其保留了大量内存。因此,如果我进行手动实施,我确信我可以完成此任务。例如:
void Fun(std::vector<double> &in, std::vector<double> &out) {
out.resize(in.size());//note - if the capacity of out is greater than in.size(), this will involve no memory release or allocation
for (unsigned int i = 0;i<in.size();i++) {
out[i] = in[i];
}
}
Run Code Online (Sandbox Code Playgroud)
如果 in.size() 小于 out 的现有容量,则后一个实现将不会释放或分配任何新的堆内存。
问题是 - 我原来的实现会做同样的事情吗?即,如果我简单地执行“out = in;”,std::vector 会知道以内存有效的方式自动执行此操作吗?
我尤其担心也许“out = in;” 行可能会执行一些操作,例如释放当前分配给 out 的所有堆内存,然后重新分配它。与此有效等效的东西:
void Fun(std::vector<double> &in, std::vector<double> &out) {
out.clear();
out.shrink_to_fit();//releasing memory from heap
out.resize(in.size());//reallocating memory from …Run Code Online (Sandbox Code Playgroud) 我对cplusplus.com上的一些文档感到困惑.我是否将标准模板库与这两行代码中的任何一行一起使用?
for (std::string::const_iterator it = str.begin(); l < data.size; ++it, ++l)
Run Code Online (Sandbox Code Playgroud)
和
tile_tag(): distance(std::numeric_limits<int>::max()), dir(unknown_direction), used(false)
Run Code Online (Sandbox Code Playgroud)
请参阅:http: //www.cplusplus.com/reference/string/string/begin/ 和 http://www.cplusplus.com/reference/std/limits/numeric_limits/
如果我使用过STL,我如何修改它们以便在没有STL的情况下完成它们的工作?
谢谢!
编辑:我想这是我们对STL的定义:http://www.sgi.com/tech/stl/stl_index_cat.html
我没有看到const_iterator ......但我确实看到了最大值.但这是我用过的最大值吗?
让我们说如果我有一个向量V,它有10个元素.如果我擦除第一个元素(在索引0处)v.erase(v.begin())然后使用STL向量如何处理它?
它是否会创建另一个新的向量并将元素从旧向量复制到新向量并释放旧向量?或者它是否从索引1开始复制每个元素并将元素复制到index-1?
如果我需要一次使用大小为100,000的向量,之后我不会使用那么多空间,我可以说我只需要一个大小为10的向量然后它会自动减小大小吗?(我不这么认为)
我在网上看了,只有API和教程如何使用STL库.是否有任何好的参考资料,我可以了解STL库的实现或复杂性?
我在使用STL的时候碰到了一个速度突发,看似正常的情况,在这里简化:
class Person {
string Name;
int Age;
};
vector<Person> people;
AddPeople(people);
string s("Bob");
find(people.begin(), people.end(), s);
Run Code Online (Sandbox Code Playgroud)
find想要比较整个班级.
有一些潜在的解决方法:
find完全放弃(混乱,但可以重构):
bool bBob = false;
for (UINT i = 0; i < people.size(); i++) {
if (people[i].Name == s)
bBob = true;
break;
}
Run Code Online (Sandbox Code Playgroud)提供转换运算符(隐式转换不起作用;显式不能使用find):
class Person {
string Name;
int Age;
operator string() {return Name;}
};
Person b ("Bob", 99);
string s ("Bob");
b == s; //doesn’t …Run Code Online (Sandbox Code Playgroud)我正在做一个个人项目,我需要一个动态工厂。目前我有以下“系统”:
class Action { ...} // Which is abstract
class A : public Action { ... }
class B : public Action { ... }
class C : public Action { ... }
...
std::map<std::string, Action *> _actions;
_action.insert( { "A", new A() } );
_action.insert( { "B", new B() } );
_action.insert( { "C", new C() } );
Action * act = _actions.find("B")->second; // Get action's pointer
Run Code Online (Sandbox Code Playgroud)
但是我对这个解决方案并不满意,因为即使我从不使用这些操作,它们也会在启动时实例化并在应用程序的所有生命周期中都保留在内存中,如果我想要一个“干净”的对象,我需要动态转换我的对象以确定它的类型以通过复制调用构造函数......我不是这个解决方案的真正粉丝,因为我必须“硬编码”所有情况......
我不知道我想象的解决方案是否真的可行,但这是我的想法:
我保留了以前的类层次结构,即:
class Action { ...} // Which is abstract
class …Run Code Online (Sandbox Code Playgroud) 标准是否保证以下代码安全std::string?
#include <string>
#include <cstdio>
int main()
{
std::string strCool = "My cool string!";
const char *pszCool = strCool.c_str();
strCool = pszCool;
printf( "Result: %s", strCool.c_str() );
}
Run Code Online (Sandbox Code Playgroud)
我见过一些声明,表明c_str只有在对同一结果std::string进行另一个方法调用之前才能保证安全使用,但我不清楚将其传递const char *回赋值方法是否安全。
当使用现实世界的编译器(GCC、Clang 和 MSVC 的最新版本)进行测试时,它们似乎都支持这种行为。
此外,编译器还支持将字符串的后缀分配回其自身,例如strCool = pszCool + 3在本例中;结果将是该字符串与传递给它的值具有相同的值。
这种行为是否以某种方式得到保证,或者我只是幸运的是我测试过的编译器提供的标准库支持这种情况?