我想排序一个 vector
vector<myClass> object;
Run Code Online (Sandbox Code Playgroud)
其中myclass包含许多int变量.如何对我vector的任何特定数据变量进行排序myClass.
考虑一下
void f(vector<const T*>& p)
{
}
int main()
{
vector<T*> nonConstVec;
f(nonConstVec);
}
Run Code Online (Sandbox Code Playgroud)
下列情况不compile.The事情是vector<T*>不能转换到vector <const T*>,这不合逻辑对我来说,因为存在从隐式转换T*到const T*.为什么是这样 ?
vector<const T*>也不能转换为vector <T*>,但这是预期的,因为const T*无法隐式转换为T*.
我遇到了这个问题,维护一个大的端口(相对于我们团队的大小)项目,但是创建一个小例子很简单.stackoverflow.cpp:
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main (int argc, char *argv[]) {
stack<const string> strstack;
string str("Hello, world");
strstack.push(str);
cout << strstack.top() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
看起来没错,对吧?MSVS也这么认为.然而:
g++ stackoverflow.cpp
In file included from /usr/include/c++/4.7/x86_64-linux-gnu/bits/c++allocator.h:34:0,
from /usr/include/c++/4.7/bits/allocator.h:48,
from /usr/include/c++/4.7/string:43,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from stackoverflow.cpp:1:
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of ‘class __gnu_cxx::new_allocator<const std::basic_string<char> >’:
/usr/include/c++/4.7/bits/allocator.h:89:11: required from ‘class std::allocator<const std::basic_string<char> >’
/usr/include/c++/4.7/bits/stl_deque.h:489:61: required from ‘class std::_Deque_base<const std::basic_string<char>, std::allocator<const std::basic_string<char> > >’
/usr/include/c++/4.7/bits/stl_deque.h:728:11: required …Run Code Online (Sandbox Code Playgroud) 请参阅下面的代码 - 我试图将const对象放入向量中.我知道答案是"STL容器要求对象可分配并复制施工的",但是,如果没有列举的标准,任何人都可以解释什么用这样的问题呢?我不明白为什么这样的类无法复制(除了c ++不允许).
它只是一个存储的值,不允许更改 - 为什么不能将它放在一个向量中只是创建另一个这些对象?
#include <vector>
// Attempt 1
// /home/doriad/Test/Test.cxx:3:8: error: non-static const member ‘const int MyClass::x’, can’t use default assignment operator
// struct MyClass
// {
// int const x;
// MyClass(int x): x(x) {}
// };
//
// int main()
// {
// std::vector<MyClass> vec;
// vec.push_back(MyClass(3));
// return 0;
// }
// Attempt 2
// /home/doriad/Test/Test.cxx:28:23: error: assignment of read-only member ‘MyClass::x’
struct MyClass
{
int const x;
MyClass(int x): x(x) {}
MyClass& …Run Code Online (Sandbox Code Playgroud) 在我使用了一些模板代码中的一些错误后,std::vector::value_type我把它跟踪到以下内容.这是根据标准的正确行为,还是这是MSVC 2012 CTP的问题?
typedef std::vector<int>::value_type t1;
typedef std::vector<int const>::value_type t2;
static_assert(!std::is_same<t1, t2>::value, "hmmm");
Run Code Online (Sandbox Code Playgroud)
上述断言失败了.
我想filter用C++ 编写一个更高阶的函数.我到目前为止提出的代码如下:
#include <iostream>
#include <string>
#include <functional>
#include <algorithm>
#include <vector>
#include <list>
#include <iterator>
using namespace std;
bool isOdd(int const i) {
return i % 2 != 0;
}
template <
template <class, class> class Container,
class Predicate,
class Allocator,
class A
>
Container<A, Allocator> filter(Container<A, Allocator> const & container, Predicate const & pred) {
Container<A, Allocator> filtered(container);
container.erase(remove_if(filtered.begin(), filtered.end(), pred), filtered.end());
return filtered;
}
int main() {
int const a[] = {23, 12, 78, 21, …Run Code Online (Sandbox Code Playgroud) 我想使用向量来保存大小为5x5的只读整数矩阵
vector<const int[5][5]> startingPieces;
Run Code Online (Sandbox Code Playgroud)
但是这个声明引起了一系列我以前从未见过的奇怪错误.
error C2535: 'const int (*std::allocator<_Ty>::address(const int (&)[5][5]) const)[5][5]' : member function already defined or declared
1> with
1> [
1> _Ty=const int [5][5]
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\xmemory(109) : see declaration of 'std::allocator<_Ty>::address'
1> with
1> [
1> _Ty=const int [5][5]
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\vector(429) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=const int [5][5]
1> ]
1> c:\program files\microsoft …Run Code Online (Sandbox Code Playgroud)