标签: vector

可以双向调整矢量大小吗?

有什么功能可以在两个方向上调整向量的大小吗?我们可以从开始添加新的空元素的地方操纵指针或元素吗?

c++ vector

-2
推荐指数
2
解决办法
90
查看次数

非初始化reference_wrapper的返回值

reference_wrapper当我resize()在下面的向量时,指向何处?这是一个未定义的行为吗?我该怎么做才能安全?

std::vector < std::reference_wrapper <int> > vec;
vec.resize(10);
Run Code Online (Sandbox Code Playgroud)

c++ vector c++11 reference-wrapper

-3
推荐指数
1
解决办法
393
查看次数

如何对vector <vector <int >>进行排序?

我有一个矢量:

vector<vector<int>> myvector;
Run Code Online (Sandbox Code Playgroud)

如何按顺序对字符串的字母顺序对此向量进行排序?

例如输出:

未排序:

5 9 4 12 4

7 9 3 4 7 9

6 5 11

5 8 7 3

5 9 5 1 1

排序方式:

7 9 3 4 7 9

6 5 11

5 9 5 1 1

5 9 4 12 4

5 8 7 3

c++ sorting vector

-3
推荐指数
1
解决办法
2119
查看次数

std :: vector <int> :: erase在编译时失败

我有一段代码,我首先定义了

std::vector<int> half_gid_m;
Run Code Online (Sandbox Code Playgroud)

我通过函数push_back计算向量half_gid_m的元素.然后我在向量上执行函数sort和unique:

sort(half_gid_m.begin(),half_gid_m.end());
std::vector<int>::iterator itv( std::unique(half_gid_m.begin(),half_gid_m.end()) );
Run Code Online (Sandbox Code Playgroud)

最后,我用重复的条目擦除向量的一部分

half_gid_m.erase(itv,half_gid_m.end());
Run Code Online (Sandbox Code Playgroud)

似乎一切都是正确的,但编译给了我这种奇怪的错误

error: no matching function for call to 'std::vector<int, std::allocator<int>    >::erase(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&, __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >&) const'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:110: note: candidates are: typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = int, _Alloc = std::allocator<int>]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:122: note:                 typename std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >) [with _Tp = int, _Alloc = std::allocator<int>]
//home/gales/ingv/p-gales/simulations/little_mesh/lib/p-gales/include/gales-0.1/gales/map/map_rule.hpp:166: …
Run Code Online (Sandbox Code Playgroud)

c++ vector

-3
推荐指数
1
解决办法
991
查看次数

std :: vector operator []与at()访问

<std :: Vector> Operator [] vs. at()

=========================================

我在某处读到,只有索引访问运算符[]和()成员函数之间的区别在于()也检查索引是否有效.但是,从以下代码中扣除,似乎存在差异

std::vector<std::string>* namesList = readNamesFile("namesList.txt");
std::vector<Rabbit> rabbits;

const int numNAMES = namesList->size();

for (int i = 0; i < 5; i++)
{
    rnd = rand() % numNAMES;
    rabbits.push_back(Rabbit(namesList[i]));
}
Run Code Online (Sandbox Code Playgroud)

上面的代码抛出

error C2440: '<function-style-cast>' : cannot convert from 'std::vector<std::string,std::allocator<_Ty>>' to 'Rabbit'
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
Run Code Online (Sandbox Code Playgroud)

此外,如果我将鼠标悬停在上面(见下文)

rabbits.push_back(Rabbit(namesList[i]));
Run Code Online (Sandbox Code Playgroud)

                                     ^^^^^^^我读了intelliSense:

Error: no instance of constructor …
Run Code Online (Sandbox Code Playgroud)

c++ vector stdvector operator-keyword

-3
推荐指数
1
解决办法
790
查看次数

当它不应该是C++时,逻辑比较失败

我一直有一个问题,这个比较if (code->at(i) == guess.at(i)) 失败了,即使它不应该因为它们都是'0':http://puu.sh/bvCRB/c640fb85a4.png

guess是炭的向量s(vector<char> guess){code是一个指向炭的矢量vector<char> * code

c++ vector

-3
推荐指数
1
解决办法
88
查看次数

c ++ std :: find自定义比较器不起作用

我现在正在学习如何使用std :: find和自定义比较器.

但是,通过在线指导,我面临编译器错误.

链接到我的代码.

以下是我的代码:

    #include <iostream>
    #include <algorithm>
    #include <pair.h>
    #include <vector>

    using namespace std;

    int main()
    {
        struct comp
        {
            comp(const int& input) : _input(input) {}
            bool operator()(const pair<int, int>& iPair)
            {
                return (iPair.first == _input);
            }
            int _input;
        };

        pair<int, int> pair1(1,3);
        pair<int, int> pair2(2,4);

        vector<pair<int, int> > vec;
        vec.push_back(pair1);
        vec.push_back(pair2);

        vector<pair<int,int> >::iterator it = find(vec.begin(), vec.end(), comp(1));
        if(it != vec.end())
        {
            cout << it->second << endl;
        }

        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

错误如下:

In function …
Run Code Online (Sandbox Code Playgroud)

c++ stl vector find custom-compare

-3
推荐指数
1
解决办法
897
查看次数

无法使用Vector编码显示名称

下午好.....我目前是C++课程的学生,我们正在编写Vector代码.在作业中,我们必须显示3个学生姓名.当我在运行以下代码后添加名称时,它只显示向量的数量而不是名称(根据需要).任何帮助表示赞赏.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>

using std::cout;
using std::cin;
using namespace std;

int main()
{
    bool running = true;
    char choice;
    vector <string> studentVector;
    cout << "My Student Vector" << endl;
    while (running == true)
    {
        cout << "Choose a Selection: [A]dd Student, [V]iew Student, [R]emove      Student, [E]xit" << endl;
        cin >> choice;
        cin.ignore();

        if (choice == 'A' || choice == 'a')
        {
            cout << "Enter Student's Name: ";
            string tempVar;
            getline(cin, tempVar);

            studentVector.push_back(tempVar);
        }
        else …
Run Code Online (Sandbox Code Playgroud)

c++ vector c++11

-3
推荐指数
1
解决办法
53
查看次数

vector :: reserve和内置类型

如果没有整体阅读,请不要将其标记为重复.这不是"std :: reserve做什么"的问题.

使用内置类型写入vector :: reserve'd地址是错误的吗?

vector<int> vec;
vec.reserve(10);
vec[5] = 24; // Is this an error?
Run Code Online (Sandbox Code Playgroud)

我知道对象没有被初始化,但由于这些只是整数而空间是由保留分配的,而这是在连续存储中完成的,这是一个错误吗?

c++ vector

-3
推荐指数
1
解决办法
79
查看次数

按数组元素排序数组的向量

我在编写解决背包问题的算法时遇到了问题.我有一个3元素数组(C++ 11)的向量,我想通过let的说明这些数组的第一个元素来对向量进行排序.

我已尝试使用预定义的比较函数进行std :: sort,但它甚至都没有编译.

我想我的比较功能不能像我期望的那样工作:

bool compareByValue(const data &a, const data &b)
{
    return a[0] < b[0];
}

int main()
{
    vector<array<int, 3> > myVector;
    ...
    sort ( myVector.begin(), myVector.end(), compareByValue );
}
Run Code Online (Sandbox Code Playgroud)

这不是我第一次遇到类似的问题,我试图在网上找到解决方案,但没有任何令人满意的结果.

c++ arrays sorting vector c++11

-3
推荐指数
1
解决办法
190
查看次数