[英]accessing elements of a vector of struct with iterators c++
我做了以下代碼:
namespace bcra {
// The value type of a data item.
using value_t = long;
/// This class represents a single Bar Chart.
class BarChart
{
//=== Definition
public:
/// Represents a single bar information.
struct BarItem
{
string label; //!< Bar label.
value_t value; //!< Bar value.
string category; //!< Bar category.
BarItem( string l, value_t v, string c ) : label{l}, value{v}, category{c}
{/*empty*/}
};
//== Data members
public:
/// The collection of bars items
std::vector< BarItem > bars;
};
}
int main()
{
BarChart m_barChart;
// open file txt
m_barChart.bars.push_back(...);
// close file txt
// ....
// ....
// ....
// creating a vector where I'm going to take a certain range from that main vector.
std::vector<BarChart::BarItem> bar_s;
for (auto i{ count_value }; i < (count_value + n_linhas); ++i)
{
bar_s.push_back(m_barChart.bars[i]);
}
// ....
// ...
// MY PROBLEM
std::vector < value_t>::iterator inicio2; ??????
std::vector<value_t>::iterator fim2; ??????
inicio2 = bar_s.begin(); ????????
//
// ...
sort(bar_s.begin(), bar_s.end(), compare);
我的目標如下:m_barChart 將擁有來自具有以下結構(字符串、整數、字符串)的文本文件中的所有數據,而我想要做的是創建一個新向量,在其中放置這個更大向量的范圍(到目前為止我已經成功了),最大的問題是我想獲取另一個向量(bar_s)並使用一個迭代器對其進行排序,該迭代器獲取整數值,交換 bar_s[0] 的所有對象(字符串,整數,字符串)使用 bar_s[1] (...),如果它更大。
為此,我需要 begin() 僅在整數值上(類似於 => m_bars.begin().value ??)???? 我不知道從這里做什么。
這在過去可以通過合適的比較函數來解決,但在現代 C++ 中,您將使用投影:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.