指针和迭代器 C++
pointer & iterator c++
我必须做一个项目。我有一个稀疏矩阵,有3个数组:
- val
- 行位置的pr
- 立柱位置的pc
现在我遇到了迭代器的问题。我必须返回一个指向包含3个内容的结构的指针:
- 价值
- 行
- 列
class const_iterator; // forward declaration
class iterator {
friend class const_iterator;
friend class sparsematrix;
public:
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
iterator() : ptr(0) {}
iterator(const iterator &other) : ptr(other.ptr) {}
iterator& operator=(const iterator &other) {
ptr = other.ptr;
return *this;
}
~iterator() {}
/**
* Operatore di dereferenziamento.
* @return il dato "puntato" dall'iteratore.
*/
reference operator*() const {
return *ptr;
}
/**
* Operatore freccia.
*
* @return il puntatore al dato "puntato" dall'iteratore.
*/
pointer operator->() const {
return ptr;
}
/**
* Operatore di confronto (uguaglianza).
*
* @param other iteratore da confrontare
* @return true se i due iteratori "puntano" allo stesso dato
*/
bool operator==(const iterator &other) const {
return ptr == other.ptr;
}
/**
* Operatore di confronto (disuguaglianza).
*
* @param other iteratore da confrontare
* @return true se i due iteratori "puntano" a dati diversi
*/
bool operator!=(const iterator &other) const {
return !(*this == other);
}
/**
* Operatore di confronto (uguaglianza).
*
* @param other const_iteratore da confrontare
* @return true se i due iteratori "puntano" allo stesso dato
*/
bool operator==(const const_iterator &other) const {
return ptr == other.ptr; // vedi const_iterator
}
/**
* Operatore di confronto (uguaglianza).
*
* @param other const_iteratore da confrontare
* @return true se i due iteratori "puntano" a dati diversi
*/
bool operator!=(const const_iterator &other) const {
return !(*this == other);
}
/**
* Operatore di "elemento successivo". Versione pre-incremento.
*
* @return l'iteratore che "punta" all'elemento successivo
*/
iterator &operator++() {
++ptr;
return *this;
}
/**
* Operatore di "elemento successivo". Versione post-incremento.
*
* @return l'iteratore che "punta" all'elemento successivo
*/
iterator operator++(int) {
iterator tmp(ptr);
++ptr;
return tmp;
}
private:
value_type *ptr;
/**
* Costruttore di inizializzazione.
* @param p puntatore ai dati
*/
explicit iterator(value_type *p) : ptr(p){}
}; // iterator
/**
* Inizio Const Iterator
*/
class const_iterator {
friend class iterator;
friend class sparsematrix;
public:
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef const T* pointer;
typedef const T& reference;
const_iterator() : ptr(0) {}
const_iterator(const const_iterator &other) : ptr(other.ptr) {}
const_iterator& operator=(const const_iterator &other) {
ptr = other.ptr;
return *this;
}
~const_iterator() {}
/**
* Costruttore di conversione.
* @param other iterator da convertire
*/
const_iterator(const iterator &other) : ptr(other.ptr) {}
/**
* Operatore di dereferenziamento.
* @return il dato "puntato" dall'iteratore.
*/
reference operator*() const {
return *ptr;
}
/**
* Operatore freccia.
* @return il puntatore al dato "puntato" dall'iteratore.
*/
pointer operator->() const {
return ptr;
}
/**
* Operatore di confronto (uguaglianza).
* @param other const_iteratore da confrontare
* @return true se i due iteratori "puntano" allo stesso dato
*/
bool operator==(const const_iterator &other) const {
return ptr == other.ptr;
}
/**
* Operatore di confronto (disuguaglianza).
* @param other const_iteratore da confrontare
* @return true se i due iteratori "puntano" a dati diversi
*/
bool operator!=(const const_iterator &other) const {
return !(*this == other);
}
/**
* Operatore di "elemento successivo". Versione pre-incremento.
* @return l'iteratore che "punta" all'elemento successivo
*/
const_iterator &operator++() {
++ptr;
return *this;
}
/**
* Operatore di "elemento successivo". Versione post-incremento.
* @return l'iteratore che "punta" all'elemento successivo
*/
const_iterator operator++(int) {
const_iterator tmp(ptr);
++ptr;
return tmp;
}
private:
const T *ptr;
/**
* Costruttore di inizializzazione.
* @param p puntatore ai dati
*/
explicit const_iterator(const T *p) : ptr(p) {}
}; // const_iterator
iterator begin() {
return iterator(val);
}
iterator end() {
return iterator(val + size);
}
const_iterator begin() const {
return const_iterator(val);
}
const_iterator end() const {
return const_iterator(val + size);
}
返回一个结构必须怎么做?对不起我糟糕的英语!
您可以在C(++)中显式地从函数返回structs。我不能简单地按照你的例子(对不起!),但你只需要将它们的返回类型定义为你的结构。例如:
typedef struct {
int a, b, c;
} MY_STRUCT_TYPE;
MY_STRUCT_TYPE returnStruct(MY_STRUCT_TYPE example) {
MY_STRUCT_TYPE returnValues;
returnValues.a=example.a*2;
returnValues.b=example.b*2;
returnValues.c=example.c*2;
return(returnValues);
}
int main()
{
struct MY_STRUCT_TYPE example;
example.a=5; example.b=10; example.c=15;
example=returnStruct(example);
//a=10; b=20; c=30
}
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++中带有List类的迭代器Segfault
- 如何在c++迭代器类型中包装std::chrono
- 集合上的输出迭代器:assign和increment迭代器
- Boost Spirit,获取迭代器内部语义动作
- 对于set上的循环-获取next元素迭代器
- 为什么output_editor Concept不需要output_e迭代器标记
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 使用迭代器时如何访问对象在向量中的位置?
- std::vector::迭代器是否可以合法地作为指针
- 跟随整数索引列表的自定义类迭代器
- 不明白迭代器,引用和指针失效,一个例子
- 我可以使用反向迭代器作为ForwardIt吗
- ESP8266单片机矢量迭代器的C++问题
- 如何在C++中将迭代器作为函数参数传递
- 是否应避免从非常量迭代器转换为常量迭代器?
- 如何在 c++ 中将字符串迭代器变量传递给函数?
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 为什么 C++ std::unordered_map 从 emplace/ 找到返回一个迭代器?