标签: stdvector

如何分配二维向量?

我在一个问题中使用,但到目前为止dfs我还没有调用主函数,我的程序崩溃了。dfs最近我在编程c,现在我切换到cpp. 所以我是新手cpp

我知道我在向量中哪里出错了,请告诉我可以改进什么。我知道矢量可以自动增加其大小。

#include<iostream>
#include<vector>
using namespace std;
const int MAX = 100000;

bool visited[MAX] = { 0 };
int intime[MAX];
int outtime[MAX];

int timer = 0;
void dfs(vector<vector<int>> graph, int v)
{
    visited[v] = true;
    timer++;
    intime[v] = timer;
    vector<int>::iterator it = graph[v].begin();
    while (it != graph[v].end()) {
        if (visited[*it] == false)
        {
            dfs(graph, *it);
        }
        it++;
    }
    ++timer;
    outtime[v] = timer;
}

int main()
{
    vector<vector<int>> graph;
    graph[1].push_back(2); …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm stdvector data-structures c++11

0
推荐指数
1
解决办法
2291
查看次数

创建派生类的 std::vector

假设我有一个抽象类

class AbstractClass {
public:
    virtual int get() const = 0;
};
Run Code Online (Sandbox Code Playgroud)

和两个不同的派生类

class DerivedClassA : public AbstractClass {
public:
    int get() const override { return 1; }
};

class DerivedClassB : public AbstractClass {
public:    
    int get() const override { return 2; }
};
Run Code Online (Sandbox Code Playgroud)

我想将std::vectorAbstract Classed 传递给给定的函数:

int f(const std::vector<std::shared_ptr<AbstractClass> >& classes) { ... }
Run Code Online (Sandbox Code Playgroud)

我正在做的是这样的:

int main () {
    std::vector<std::shared_ptr<AbstractClass> > _classes;
    std::shared_ptr<AbstractClass> _derivedA = std::make_shared<DerivedClassA>();
    _classes.push_back(_derivedA);
    std::shared_ptr<AbstractClass> _derivedB = std::make_shared<DerivedClassB>();
    _classes.push_back(_derivedB);
    std::cout << f(_classes) << …
Run Code Online (Sandbox Code Playgroud)

c++ derived-class stdvector

0
推荐指数
1
解决办法
291
查看次数

我可以从迭代器获取容器对象吗?

std::vector<int> vec={1,2,3};
std::vector<int>::iterator it = vec.begin();

if(vec == get_vec_from_it(it)){
  puts('sucesss');
}
Run Code Online (Sandbox Code Playgroud)
std::vector<int> get_vec_from_it(std::vector<int>::iterator it){
/*?*/
}

Run Code Online (Sandbox Code Playgroud)

get_vec_from_it上面例子中的函数应该怎么写?

c++ stl stdvector

0
推荐指数
1
解决办法
515
查看次数

std::vector.data() 在指针向量上是否以 null 终止?

我在我的 C++ 应用程序中使用 C 库。其中一项功能需要一个null-terminated array of pointers.

由于我使用的是 C++,因此我将数组的元素存储在std::vector.

data()我想知道简单地调用我的向量并将结果传递给库函数是否安全。

示例:

std::vector<struct A *> vec;

//library_function(struct A **array);
library_function(vec.data());
Run Code Online (Sandbox Code Playgroud)

当然,如果我更改向量中的元素,则返回的指针data()将无效,但这不是这里的问题(我可以在更新向量时再次调用该函数)。

我只是担心这可能是未定义的行为,因为我看不到任何提到的data()由空指针而不是随机垃圾终止的地方。

标准确实说:

const T* data() const noexcept;

Returns pointer to the underlying array serving as element storage.
The pointer is such that range [data(); data() + size()) is always a valid range
Run Code Online (Sandbox Code Playgroud)

因此存在一个终止元素,但它没有说明该元素是否已初始化以及如果已初始化,则具有什么值。

是否在其他地方指定了我错过的?
或者我是否必须自己分配一个原始数组并以 null 终止它?

c++ std stdvector undefined-behavior

0
推荐指数
1
解决办法
728
查看次数

组合具有相似字符串值的对向量 C++

我有一个向量对,其中包含字符串作为标题和 int 作为值。我想添加具有相似标题的所有值。我想知道是否std::accumulate有办法探索这个,或者是否std::map也可以使用。

所以我有一个成对的向量: std::vector<std::pair<std::string, int>> list { {"a",10},{"a",20},{"a",30},{"b",5},{"c",4},{"d",10},{"a",10},{"f",11},{"d",15},{"a",20} };它应该减少到{{"a",70},{"b",5},{"c",4},{"d",25},{"f",11}}相似字符串添加其值的位置。

这是我到目前为止所拥有的,但是j当有后续的类似标题时,我的迭代器会跳过。

for (std::size_t i = 0; i < list.size(); ++i) {
    for (std::size_t j = i + 1; j < list.size(); ++j) {
        if (list[i].first == list[j].first) {
            list[i].second += list[j].second;
            list.erase(list.begin() + j);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望有所启发。谢谢你!

c++ stdvector std-pair

0
推荐指数
1
解决办法
541
查看次数

向量只包含一个元素

我正在用 C++ 制作一个文本冒险,作为学习该语言的尝试。玩家使用“看”“向北行进”等命令进行交互。到目前为止,我已经得到了将文本转换为小写然后将其拆分为向量的代码。这对于单字命令来说效果很好,但是当我去实现更长的命令时,我发现长度总是 = 1 并且访问它会返回错误。这是我的代码:

//Input function.
void Input() {
    const char delim = ' ';

    //Processing
    std::cin >> input;
    std::transform(input.begin(), input.end(), input.begin(),
        [](unsigned char c) { return std::tolower(c); });
    std::vector<std::string> out;
    split(input, ' ', out);

    //Commands
    if (out[0] == "exit")
        exit(0);
    else if (out[0] == "help")
        Help();
    else if (out[0] == "look")
        Look();
    else if (out[0] == "travel" || out[0] == "go") {
        //This code will never run.
        if (int(out.size()) == 2) {
            Travel(out[1]); …
Run Code Online (Sandbox Code Playgroud)

c++ std stdvector jsoncpp

0
推荐指数
1
解决办法
289
查看次数

为什么 std::vector 在 C++ 中没有给出任何输出

我不明白为什么,但在我将类指针放入数组后, std::vector 没有给出任何内容。

// runs at start
void States::AssignState(GameState* state) {
    _nextVacentState++;
    _states.push_back(state);
}

// executes in a loop
void States::ExecuteCurrentState() {
    // protection incase there is nothing in the array or the current state is not grater than the size of the array (not the problem after i nerrowed the problem down)
    if (_nextVacentState == 0) std::cout << "Error: There is no states, setup some states then try again" << std::endl; return; // there is no states
    if (_currentState …
Run Code Online (Sandbox Code Playgroud)

c++ pointers class stdvector

0
推荐指数
1
解决办法
82
查看次数

将成员从向量 &lt;Class&gt; 复制到向量 &lt;Member_Type&gt; 的最佳方法

我有一个复杂结构的向量(此处std::pair<int, int>)。我现在想将一个成员(比如说std::pair::first)复制到一个新的向量中。有没有比这更好(更惯用)的方法

std::vector<std::pair<int, int>> list = {{1,2},{2,4},{3,6}};

std::vector<int> x_values;
x_values.reserve(list.size());
for( auto const& elem : list )
{
    x_values.emplace_back(elem.first);
}
Run Code Online (Sandbox Code Playgroud)

c++ algorithm copy std stdvector

0
推荐指数
1
解决办法
112
查看次数

有没有办法在 C++ 中一次迭代两个项目?

想象一下我有一个有序的 std::vectorA = {x1, x2, ..., xn}并且我想对每个后续的项目对执行操作,例如f(x1, x2); f(x2, x3); ... f(xn-1, xn); f(xn, x1)

我可以像平常一样迭代,同时跟踪前一项:

for (auto x : A) {
    ...
    f(previous_x, x);
    previous_x = x;
}

f(previous_x, first_x);
Run Code Online (Sandbox Code Playgroud)

但是有没有更好的方法来迭代这个向量呢?该语言中是否有可以简化此操作的功能?

尝试了提供的解决方案。它有效,但很想知道是否有更干净、更简洁的方法。

c++ loops for-loop stl stdvector

0
推荐指数
1
解决办法
702
查看次数

与 C++ 交换函数混淆: std::vector&lt;int&gt;().swap(search_indices);

这是代码,我很困惑。swap函数通常用于交换两个参数的值,例如a.swap(b)swap(a, b)。这里有什么意义呢swap

std::vector<int> search_indices;
        std::vector<float> distances;

        int keypointNum = 0;
        do
        {
            keypointNum++;
            std::vector<int>().swap(search_indices);
            std::vector<float>().swap(distances);

            int id;
            iterUnseg = unVisitedPtId.begin();
            id = *iterUnseg;
            indices->indices.push_back(features[id].ptId);
            unVisitedPtId.erase(id);

            tree.radiusSearch(features[id].pt, _curvature_non_max_radius, search_indices, distances);

            for (int i = 0; i < search_indices.size(); ++i)
            {
                unVisitedPtId.erase(search_indices[i]);
            }

        } while (!unVisitedPtId.empty()); 
Run Code Online (Sandbox Code Playgroud)

我查了一下swap函数是如何工作的,没有相关的解释。

c++ swap stdvector

0
推荐指数
1
解决办法
221
查看次数