auto

auto 迭代器的使用

我是研究僧i 提交于 2019-12-02 01:49:25
这种写法错误,迭代器不能向下递减,小于零后,k的值会越界变得非常大 可以使用reverse()包含在algorihtm 头文件下的函数将数据反转 来源: https://www.cnblogs.com/zxzmnh/p/11724134.html

auto reference in c++11

空扰寡人 提交于 2019-12-02 01:28:57
问题 I have some trouble about auto reference. const int i = 1; auto & ri1 = i; auto & ri2 = 1; //error Why is deduced type of ri1 const int but not ri2 ? Thanks! 回答1: Since i has type const int , but 1 has type int . 来源: https://stackoverflow.com/questions/14008893/auto-reference-in-c11

why auto i = same_const_variable could not deduce “const”?

梦想与她 提交于 2019-12-02 00:47:23
问题 const int ci = 10; auto i = ci; // i will be "int" instead of "const int" i = 20; I am wondering why auto is designed for this kind of behaviour? why the type i is "int" instead of "const int" ? what is the concern here? I think understand why will help us to remember it 回答1: auto mostly follows the same type deduction rules as template argument deduction. The only difference is that auto will deduce std::initializer_list from a braced-init-list in some cases, while template argument

inconsistent behavior of boost spirit grammar [duplicate]

落爺英雄遲暮 提交于 2019-12-02 00:11:00
问题 This question already has an answer here : undefined behaviour somewhere in boost::spirit::qi::phrase_parse (1 answer) Closed 4 years ago . I have a little grammar that I want to use for a work project. A minimum executable example is: #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #pragma GCC diagnostic ignored "-Wunused-variable" #include <boost/spirit/include/karma.hpp> #include <boost/spirit

why auto i = same_const_variable could not deduce “const”?

半城伤御伤魂 提交于 2019-12-01 21:22:01
const int ci = 10; auto i = ci; // i will be "int" instead of "const int" i = 20; I am wondering why auto is designed for this kind of behaviour? why the type i is "int" instead of "const int" ? what is the concern here? I think understand why will help us to remember it Praetorian auto mostly follows the same type deduction rules as template argument deduction. The only difference is that auto will deduce std::initializer_list from a braced-init-list in some cases, while template argument deduction doesn't do this. From N3337, §7.1.6.4 [dcl.spec.auto] 6 ... The type deduced for the variable d

C++中STL中简单的Vector的实现

半城伤御伤魂 提交于 2019-12-01 17:34:14
该vector只能容纳标准库中string类, 直接上代码了,StrVec.h文件内容为: 1 #ifndef STRVEC_H 2 #define STRVEC_H 3 4 #include<iostream> 5 #include<string> 6 #include<memory> 7 using namespace std; 8 class StrVec{ 9 public: 10 // 默认构造函数 11 StrVec():elements(nullptr),first_free(nullptr),cap(nullptr) 12 { 13 14 } 15 //拷贝构造函数 16 StrVec(const StrVec& ); 17 //拷贝赋值运算符 18 StrVec& operator=(const StrVec&); 19 ~StrVec(); 20 void push_back(const string&); 21 size_t size() const { return first_free - elements ; } 22 size_t capacity() const { return cap - elements ;} 23 string* begin() const { return elements ;} 24 string* end() const

Is vector<auto> not allowed ? (error: invalid use of ‘auto’)

自闭症网瘾萝莉.ら 提交于 2019-12-01 17:28:36
I have: #include <cstdlib> #include <vector> using namespace std; int main() { auto a = -SOME_CONST_MAX; vector<auto> myVec {a, a, a, a}; } I don't know the type of the SOME_CONST_MAX but I want to make a vector of the type of -SOME_CONST_MAX . I assumed vector<auto> would work as it will deduce from type of a . I'm getting these errors running: g++ -std=c++14 main.cpp main.cpp:9:9: error: invalid use of ‘auto’ vector<auto> myVec {a, a, a, a}; ^ main.cpp:9:13: error: template argument 1 is invalid vector<auto> myVec {a, a, a, a}; ^ main.cpp:9:13: error: template argument 2 is invalid main.cpp

auto keyword behavior with references

孤街浪徒 提交于 2019-12-01 17:05:57
问题 Let's say I have a simple c++ class that contains a private member and a getter: class MyClass { private: double m_testValue = 1; public: double& getTestValue(){return m_testValue;} } Now let's say I want to call the getter to get my reference and edit this value (and printing before / after values) auto testVal = myClassInstance.getTestValue(); std::cout << myClassInstance.getTestValue() << std::endl; std::cout << testVal << std::endl; testVal = 3; std::cout << myClassInstance.getTestValue()

auto keyword behavior with references

ⅰ亾dé卋堺 提交于 2019-12-01 17:04:38
Let's say I have a simple c++ class that contains a private member and a getter: class MyClass { private: double m_testValue = 1; public: double& getTestValue(){return m_testValue;} } Now let's say I want to call the getter to get my reference and edit this value (and printing before / after values) auto testVal = myClassInstance.getTestValue(); std::cout << myClassInstance.getTestValue() << std::endl; std::cout << testVal << std::endl; testVal = 3; std::cout << myClassInstance.getTestValue() << std::endl; std::cout << testVal << std::endl; The output is 1 1 1 3 This is not exactly what I

Is vector<auto> not allowed ? (error: invalid use of ‘auto’)

点点圈 提交于 2019-12-01 16:38:25
问题 I have: #include <cstdlib> #include <vector> using namespace std; int main() { auto a = -SOME_CONST_MAX; vector<auto> myVec {a, a, a, a}; } I don't know the type of the SOME_CONST_MAX but I want to make a vector of the type of -SOME_CONST_MAX . I assumed vector<auto> would work as it will deduce from type of a . I'm getting these errors running: g++ -std=c++14 main.cpp main.cpp:9:9: error: invalid use of ‘auto’ vector<auto> myVec {a, a, a, a}; ^ main.cpp:9:13: error: template argument 1 is