Difference between revisions of "cpp/container/vector/assign"
m (langlinks) |
|||
Line 2: | Line 2: | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | ||
+ | #include <iostream> | ||
+ | #include <iomanip> | ||
+ | #include "Mensaje.hpp" | ||
+ | |||
+ | |||
+ | Mensaje::Mensaje() = default; | ||
+ | |||
+ | Mensaje::Mensaje(std::vector<uint8_t> bytes) : _bytes(bytes) {} | ||
+ | |||
+ | std::string Mensaje::toString() const { | ||
+ | std::ostringstream os; | ||
+ | os << "[(" << _bytes.size() << ") "; | ||
+ | for (unsigned _byte : _bytes) { | ||
+ | os << std::hex << std::setw(2) << std::setfill('0') | ||
+ | << _byte << " "; | ||
+ | } | ||
+ | os << "]"; | ||
+ | return os.str(); | ||
+ | } | ||
+ | |||
+ | unsigned Mensaje::size() const { | ||
+ | return _bytes.size(); | ||
+ | } | ||
+ | |||
+ | std::vector<uint8_t>::const_iterator Mensaje::begin() const { | ||
+ | return _bytes.begin(); | ||
+ | } | ||
+ | |||
+ | std::vector<uint8_t>::const_iterator Mensaje::end() const { | ||
+ | return _bytes.end(); | ||
+ | } | ||
+ | |||
+ | void Mensaje::erase(std::vector<uint8_t>::const_iterator begin, | ||
+ | std::vector<uint8_t>::const_iterator end) { | ||
+ | _bytes.erase(begin, end); | ||
+ | } | ||
+ | |||
+ | void Mensaje::insert(std::vector<uint8_t>::const_iterator beginRTU, | ||
+ | std::vector<uint8_t>::const_iterator beginOriginal, | ||
+ | std::vector<uint8_t>::const_iterator endOriginal) { | ||
+ | _bytes.insert(beginRTU, beginOriginal, endOriginal); | ||
+ | } | ||
+ | |||
+ | uint8_t Mensaje::getByteAt(unsigned ind) const { | ||
+ | return _bytes.at(ind); | ||
+ | } | ||
+ | |||
+ | void Mensaje::setByteAt(unsigned ind, uint8_t dato) { | ||
+ | _bytes.at(ind) = dato; | ||
+ | } | ||
+ | |||
+ | void Mensaje::pushByte_back(uint8_t dato) { | ||
+ | _bytes.push_back(dato); | ||
+ | } | ||
+ | |||
+ | bool Mensaje::crcOK() { | ||
+ | if (crc16(_bytes)) // Si el crc es distinto de cero | ||
+ | return false; // No contiene el crc | ||
+ | else | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | void Mensaje::aniadeCRC() { | ||
+ | if (!crcOK()) { // Si no tiene crc | ||
+ | uint16_t crc = crc16(_bytes); | ||
+ | pushWord_back(crc); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | uint16_t Mensaje::getWordAt(unsigned ind) const { | ||
+ | return (uint16_t)getByteAt(ind) << 8 | getByteAt(ind + 1); | ||
+ | } | ||
+ | |||
+ | void Mensaje::setWordAt(unsigned ind, uint16_t dato) { | ||
+ | setByteAt(ind, dato >> 8); // msb | ||
+ | setByteAt(ind + 1, dato & 0xFF); // lsb | ||
+ | } | ||
+ | |||
+ | void Mensaje::pushWord_back(uint16_t dato) { | ||
+ | pushByte_back(dato >> 8); // msb | ||
+ | pushByte_back(dato & 0xFF); // lsb | ||
+ | } | ||
+ | |||
+ | uint16_t Mensaje::crc16(std::vector<uint8_t> mensaje, unsigned len) { | ||
+ | uint16_t crc = 0xFFFF; | ||
+ | |||
+ | for(unsigned ba = 0; ba < len; ba++) { | ||
+ | crc ^= mensaje.at(ba); | ||
+ | for(unsigned i = 0; i < 8; i++) { | ||
+ | if(crc & 0x0001) { | ||
+ | crc = (crc >> 1) ^ 0xA001; | ||
+ | } else { | ||
+ | crc >>= 1; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | // pasamos a big-endian | ||
+ | return (crc >> 8) | ((crc & 0xff) << 8); | ||
+ | } | ||
+ | |||
+ | uint16_t Mensaje::crc16(std::vector<uint8_t> mensaje) { | ||
+ | return crc16(mensaje, mensaje.size()); | ||
+ | } | ||
+ | |||
+ | std::ostream& operator<<(std::ostream& os, Mensaje& mensaje) { | ||
+ | os << mensaje.toString(); | ||
+ | return os; | ||
+ | } |
Revision as of 06:39, 4 December 2020
void assign( size_type count, const T& value ); |
(1) | (constexpr since C++20) |
template< class InputIt > void assign( InputIt first, InputIt last ); |
(2) | (constexpr since C++20) |
void assign( std::initializer_list<T> ilist ); |
(3) | (since C++11) (constexpr since C++20) |
Replaces the contents of the container.
[
first,
last)
.
This overload has the same effect as overload (1) if |
(until C++11) |
This overload participates in overload resolution only if |
(since C++11) |
All iterators (including the end()
iterator) and all references to the elements are invalidated.
Contents |
Parameters
count | - | the new size of the container |
value | - | the value to initialize elements of the container with |
first, last | - | the range to copy the elements from |
ilist | - | std::initializer_list to copy the values from |
Complexity
Example
The following code uses assign
to add several characters to a std::vector<char>:
#include <vector> #include <iostream> #include <string> int main() { std::vector<char> characters; auto print_vector = [&]() { for (char c : characters) std::cout << c << ' '; std::cout << '\n'; }; characters.assign(5, 'a'); print_vector(); const std::string extra(6, 'b'); characters.assign(extra.begin(), extra.end()); print_vector(); characters.assign({'C', '+', '+', '1', '1'}); print_vector(); }
Output:
a a a a a b b b b b b C + + 1 1
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2209 | C++98 | the replacement operation was required to be implemented as erasing all existing elements followed by inserting the given elements |
removed the requirement |
See also
(C++23) |
assigns a range of values to the container (public member function) |
assigns values to the container (public member function) |
- include <iostream>
- include <iomanip>
- include "Mensaje.hpp"
Mensaje::Mensaje() = default;
Mensaje::Mensaje(std::vector<uint8_t> bytes) : _bytes(bytes) {}
std::string Mensaje::toString() const {
std::ostringstream os; os << "[(" << _bytes.size() << ") "; for (unsigned _byte : _bytes) { os << std::hex << std::setw(2) << std::setfill('0') << _byte << " "; } os << "]"; return os.str();
}
unsigned Mensaje::size() const {
return _bytes.size();
}
std::vector<uint8_t>::const_iterator Mensaje::begin() const {
return _bytes.begin();
}
std::vector<uint8_t>::const_iterator Mensaje::end() const {
return _bytes.end();
}
void Mensaje::erase(std::vector<uint8_t>::const_iterator begin,
std::vector<uint8_t>::const_iterator end) { _bytes.erase(begin, end);
}
void Mensaje::insert(std::vector<uint8_t>::const_iterator beginRTU,
std::vector<uint8_t>::const_iterator beginOriginal, std::vector<uint8_t>::const_iterator endOriginal) { _bytes.insert(beginRTU, beginOriginal, endOriginal);
}
uint8_t Mensaje::getByteAt(unsigned ind) const {
return _bytes.at(ind);
}
void Mensaje::setByteAt(unsigned ind, uint8_t dato) {
_bytes.at(ind) = dato;
}
void Mensaje::pushByte_back(uint8_t dato) {
_bytes.push_back(dato);
}
bool Mensaje::crcOK() {
if (crc16(_bytes)) // Si el crc es distinto de cero return false; // No contiene el crc else return true;
}
void Mensaje::aniadeCRC() {
if (!crcOK()) { // Si no tiene crc uint16_t crc = crc16(_bytes); pushWord_back(crc); }
}
uint16_t Mensaje::getWordAt(unsigned ind) const {
return (uint16_t)getByteAt(ind) << 8 | getByteAt(ind + 1);
}
void Mensaje::setWordAt(unsigned ind, uint16_t dato) {
setByteAt(ind, dato >> 8); // msb setByteAt(ind + 1, dato & 0xFF); // lsb
}
void Mensaje::pushWord_back(uint16_t dato) {
pushByte_back(dato >> 8); // msb pushByte_back(dato & 0xFF); // lsb
}
uint16_t Mensaje::crc16(std::vector<uint8_t> mensaje, unsigned len) {
uint16_t crc = 0xFFFF;
for(unsigned ba = 0; ba < len; ba++) { crc ^= mensaje.at(ba); for(unsigned i = 0; i < 8; i++) { if(crc & 0x0001) { crc = (crc >> 1) ^ 0xA001; } else { crc >>= 1; } } } // pasamos a big-endian return (crc >> 8) | ((crc & 0xff) << 8);
}
uint16_t Mensaje::crc16(std::vector<uint8_t> mensaje) {
return crc16(mensaje, mensaje.size());
}
std::ostream& operator<<(std::ostream& os, Mensaje& mensaje) {
os << mensaje.toString(); return os;
}