C++vector怎么用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++vector怎么用相关的知识,希望对你有一定的参考价值。
#include "iostream"
#include "vector.h"
using namespace std;
int main()
Vector<int> vec;
vec.add(8);
vec.add(9);
vec.insert(1,7);
for(int i=0;vec[i]!='\0';i++)
cout<<vec[i];
vec.remove(0);
vec.set(2,6);
cout<<vec.get(2)<<'\n'<<vec[0]<<'\n'<<vec.size();
vec[0]=3;
cout<<vec[0];
return 0;
在windows下用GCC编译得到
D:\花气袭人知昼暖\GCC>g++ vector.cpp
In file included from D:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/vector.h:59,
from vector.cpp:2:
D:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
vector.cpp: In function `int main()':
vector.cpp:7: error: 'class std::vector<int, std::allocator<int> >' has no member named 'add'
vector.cpp:8: error: 'class std::vector<int, std::allocator<int> >' has no member named 'add'
vector.cpp:9: error: no matching function for call to `std::vector<int, std::allocator<int> >::insert(int, int)'
D:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/vector.tcc:92: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]
D:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_vector.h:612: note: void std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, size_t, const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]
vector.cpp:13: error: 'class std::vector<int, std::allocator<int> >' has no member named 'remove'
vector.cpp:14: error: 'class std::vector<int, std::allocator<int> >' has no member named 'set'
vector.cpp:15: error: 'class std::vector<int, std::allocator<int> >' has no member named 'get'
vector.cpp:20:2: warning: no newline at end of file
请问这是怎么回事
c++用sort对vector排序问题
#include<string>
#include<algorithm>
#include <vector>
using namespace std;
vector<string> a;
int n = 7;
bool compare(string a, string b)
for(int i = 0; i < n - 1; ++ i)
if(a[i] < b[i])
return true;
return false;
int main()
a.push_back("+-+--+");
a.push_back("-+-+--");
a.push_back("- ++ ");
a.push_back("- ++++");
sort(a.begin(), a.end(), compare);
return 0;
运行时错误,请问怎么回事。。。
就是sort那里中断了,我的是VS2010,出错信息是在 a[i] < b[i] 那里,无效的运算符 < ,我是想按照ASCII码大小来比较string中的每一位
或者直接 通过string 里面重载<
#include<string>
#include<algorithm>
#include <vector>
using namespace std;
vector<string> a;
int n = 7;
bool compare(string a, string b)
if(a<b)
return true;
return false;
int main()
a.push_back("+-+--+");
a.push_back("-+-+--");
a.push_back("- ++ ");
a.push_back("- ++++");
sort(a.begin(), a.end(), compare);
return 0;
参考技术A 应该是字符串长度不够,导致越界,直接用string的compare函数即可
return a.compare(b); 参考技术B 我在我的电脑上编译执行了,程序没有错,所以应该是你的电脑环境有问题。
你可以单步调试你的程序,看是哪一行出错了。追问
就是sort那里中断了,我的是VS2010,出错信息是在 a[i] < b[i] 那里,无效的运算符 < ,我是想按照ASCII码大小来比较string中的每一位
追答我搜看了一下,sort你要传递一个函数给他,但是这个函数不是普通的函数(function),而是仿函数(functor),仿函数是C++编程里的一个概念,就是说,一个类,它内部重载了小括号运算符,就称为仿函数。
建议你搜一下 STL sort 中的比较函数 ,看看别人怎么写的仿函数。
以上是关于C++vector怎么用的主要内容,如果未能解决你的问题,请参考以下文章