标签: stdvector

引用vector :: front有效,但vector :: begin却没有

我有这段代码:

cerr << client->inventory.getMisc().front()->getName() << endl;
vector<itemPtr>::iterator it;
it = client->inventory.getMisc().begin();
cerr << (*it)->getName() << endl;
Run Code Online (Sandbox Code Playgroud)

让我解释一下:

client是一个tr1::shared_ptr指向一个对象,该对象具有一个名为的成员inventory,该vector<itemPtr>成员可以访问私有成员getMisc().itemPtr是一个typedef tr1::shared_ptr<Item>,并getName()返回一个私有std::string成员Item.

从本质上讲,client->inventory.getMisc()归结为a std::vector,我试图获得第一个元素的迭代器.

问题是第四行是segfaults.显然,迭代器或它指向的shared_ptr都是无效的.我使用第一个cerr语句来测试向量本身是否有效,并且它应该打印,所以我认为它是.

有什么我做错了吗?或者,你们会做什么来调试这个?

c++ segmentation-fault stdvector

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

对于const成员,如何在没有operator =()的情况下使用push_back?

如何将push_back()转换为C++ std :: vector而不使用默认定义违反const成员的operator =()?

struct Item {
  Item(int value)
    : _value(value) {
  }
  const char _value;
}

vector<Item> items;

items.push_back(Item(3));
Run Code Online (Sandbox Code Playgroud)

我想保留_value const,因为它在构造对象后不应该改变,所以问题是如何在不调用operator =()的情况下用元素初始化我的向量?

这是g ++ v3.4.6给我的基本错误:

.../3.4.6/bits/vector.tcc: In member function `Item& Item::operator=(const Item&)':
.../3.4.6/bits/vector.tcc:238:   instantiated from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Item, _Alloc = std::allocator<Item>]'
.../3.4.6/bits/stl_vector.h:564:   instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Item, _Alloc = std::allocator<Item>]'
item.cpp:170:   instantiated from here
.../3.4.6/bits/vector.tcc:238: error: non-static const member `const char Item::_value', can't …
Run Code Online (Sandbox Code Playgroud)

c++ gcc const stdvector assignment-operator

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

按偶数和奇数排序

我想知道是否可以使用std :: sort函数对偶数或奇数进行排序.

我有以下代码,但我不知道如何在std :: sort中实现

inline bool isEven(const Point n) {
return n.getX()%2==0;
}
Run Code Online (Sandbox Code Playgroud)

它是否正确

vector<Point> c;
std::sort(c.begin(),c.end(),isEven)
Run Code Online (Sandbox Code Playgroud)

请指教.

c++ sorting stdvector

3
推荐指数
2
解决办法
4442
查看次数

如何将对象的属性复制到向量?

我有一个vector<Person>想要将每个对象的属性名称提取到a vector<string>.for-each循环使它正常工作,但有没有更好的方法来使用类似的东西std::copy()并告诉他使用特定的方法(在这种情况下是name()getter)将值复制到我的vector<string>?

class Person {
  int a;
  string n;
public:
  Person(int age, string name) : a(age), n(name) {}
  int age() const { return a; }
  string name() const { return n; }
};

vector<Person> ppl = { Person(12, "Tim"), Person(21, "Tom") };
vector<string> names;

// question
// copy(ppl.begin(), ppl.end(), ????.name());

for (Person& p : ppl)
  names.push_back(p.name());

copy(names.begin(), names.end(), ostream_iterator<string>(cout, ", "));
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl stdvector

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

c ++ std :: vector <> vs new []性能

在固定和相同长度之间std::vector<>和之间的分配,解除分配和访问时间是否有任何重大差异new[]?

c++ new-operator stdvector

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

使用`for(auto&e:cont)`是否安全?vector <bool>有什么问题?

我发现for (auto& e : cont)有时使用而不是普通for (auto e : cont)(cont例如,某些容器std::vector).到目前为止,我找到了两个原因:

  1. 参考应避免复制对象(执行速度更快)
  2. 某些课程可能禁止制作副本(例如std::thread)

经过几次测试,我可以看到:

  1. for (auto& e : cont)适用于任何std::vector<T>除外std::vector<bool>
  2. for (cont::reference e : cont)适用于任何std::vector<T> 包括std::vector<bool>(这里显而易见的问题是:
    我应该使用它而不是for (auto& e : cont)?
  3. std::vector<bool>不被认为是真正的容器,许多人认为它应该被重命名(实现很好,很有用,但应该有不同的名称,如bitset或bitfield或dynamic_bitset)
  4. std::vector<bool>可以以一种for (auto& e : cont)可行的方式实现(参见下面的尝试)

这是我用于测试的代码:(
诀窍是使用reference& iterator::operator * () { return *this; })

#include <vector>
#include <iostream>
#include …
Run Code Online (Sandbox Code Playgroud)

c++ boolean stdvector auto c++11

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

如何使用一行代码连接两个向量

我发誓这不是任何看似无限数量的矢量连接线程的重复.对于我的情况,在派生类构造函数中,我需要将a传递std::vector<int>给基类构造函数,但传递的向量需要是另外两个向量的串联.例:

#include <vector>    
using namespace std;

struct Base {
    Base(vector<int> numbers) {
        //Do something with numbers
    }
};

struct Derived: public Base {
    Derived(vector<int> numbers):
        Base(concatenate(numbers, {4,5,6})) {}  //Is there a built-in "concatenate" function?
}; 

int main (int argc, char* argv[])
{
    Derived D({1,2,3});
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我显然可以通过编写自己的concatenate函数来实现这一点,但我想知道是否已经有了标准库方法来实现这一点.我发现的矢量级联的例子都不适合在初始化列表中使用,因为它们跨越多行; 我需要一个单线串联.

c++ stdvector

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

C++使用构造函数填充的向量的奇怪行为

我正在经历一些使用构造函数填充向量值的"奇怪行为".

所以,我有这个块类,只要调用构造函数,就会填充这个名为blockList的静态向量.

static std::vector<Block*> blockList;

Block::Block(float x, float y)
    : RectangleShape(sf::Vector2f(WIDTH, HEIGHT)) {
    blockList.push_back(this);
    setFillColor(sf::Color(221, 221, 221, 255));
    setPosition(x, y);
}
Run Code Online (Sandbox Code Playgroud)

现在,当我在另一个类中填充值时,我称之为"游戏"; 在它的构造函数中如下:

Game::Game(sf::RenderWindow& window) {
    // other code here

    Block firstBlock(10, 10);
    // possible more blocks here
}
Run Code Online (Sandbox Code Playgroud)

然后试图画画,它崩溃了.另外,该块的坐标打印为(x:9.8439e-12,y:9.84394e-12).现在我尝试做的是把它扔进draw方法(我知道它很糟糕,因为它经常被调用但是出于调试目的):

void Game::drawObjects() {
    // draw other shapes here

    Block oneBlock(50.0f, 50.0f);

    std::cout << std::string(10, '-') << std::endl;
    for (Block* block : Block::getBlockList()) {
        std::cout << block->getPosition().x << " - " << block->getPosition().y << std::endl;
        window->draw(*block);
    }
    std::cout << std::string(10, '-') …
Run Code Online (Sandbox Code Playgroud)

c++ runtime-error vector stdvector

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

C++ 11多个移动构造函数调用

我编写了这个,g++ -std=c++11 file.cpp我很困惑通过研究移动构造函数C++.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class P {
public:
    string* ptr_;
    P(string name) { ptr_ = new string(name); }
    ~P() { delete ptr_; }
    P(P&& pother) : ptr_(move(pother.ptr_)) { 
        cout<<"move"<<endl; 
        pother.ptr_=nullptr; 
    }
    void print() {cout << *ptr_ << endl;}
};

int main()
{
    vector<P> ppl;
    ppl.push_back(P("Jojo"));
    ppl.push_back(P("Jojo"));
    ppl.push_back(P("Jojo"));
}
Run Code Online (Sandbox Code Playgroud)

该程序的输出是:

$ ./a.out 
move
move
move
move
move
move
Run Code Online (Sandbox Code Playgroud)

为什么移动构造函数在这里调用了6次?

c++ vector stdvector c++11

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

如何从vector &lt;char&gt;转换为数字整数

我有一个向量,该向量来自用户输入60,000的['6''0''0''0''0']。我需要一个整数60000,以便可以操纵此数字。

我是C ++和一般编程的新手。我从串行端口读取了60,000-3,500,000的数据/数字,我需要一个整数,成功完成此操作并打印出来的唯一方法是通过std :: vector。我尝试做向量,但是它给了我一些时髦的数字。

#include "SerialPort.h"
std::vector<char> rxBuf(15);
DWORD dwRead;
while (1) {
  dwRead = port.Read(rxBuf.data(), static_cast<DWORD>(rxBuf.size()));
  // this reads from a serial port and takes in data
  // rxBuf would hold a user inputted number in this case 60,000
  if (dwRead != 0) {
    for (unsigned i = 0; i < dwRead; ++i) {
      cout << rxBuf[i];
      // this prints out what rxBuf holds
    }
    // I need an int = 60,000 from my vector holding [ '6' …
Run Code Online (Sandbox Code Playgroud)

c++ type-conversion char stdvector visual-c++

3
推荐指数
2
解决办法
143
查看次数