0
#include <iostream>
#include <array>
#include <vector>
using namespace std;

constexpr std::size_t Count{5U};

constexpr std::uint8_t Set{0x00U};
constexpr std::uint8_t Unset{0x01U};

constexpr std::uint8_t First{0x01U};
constexpr std::uint8_t Second{0x03U};
constexpr std::uint8_t Third{0x05U};
constexpr std::uint8_t Fourth{0x1DU};
constexpr std::uint8_t Fifth{0x27U};


constexpr std::array<std::pair<std::uint8_t, std::uint8_t>, Count> buffer{
    std::make_pair(First, Set), std::make_pair(Second, Unset),
    std::make_pair(Third, Unset), std::make_pair(Fourth, Unset),
    std::make_pair(Fifth, Unset)};

int main()
{
    cout<<"Hello World"<<endl;
    
    std::vector<std::uint8_t> reply;
    std::vector<std::uint8_t>::iterator it;
    
    cout<<"reply size is "<<reply.size()<<" and capacity is "<<reply.capacity()<<endl;
    reply.clear();
    reply.reserve(1U + (Count * 2U));
    cout<<"reply size after reserve is "<<reply.size()<<" and capacity is "<<reply.capacity()<<endl;

    reply.push_back(static_cast<std::uint8_t>(Count));
    cout<<"reply size after resize is "<<reply.size()<<endl;
    
    cout<<"data is "<<reply.at(0)<<reply[0]<<reply.front()<<endl;

    for (std::size_t i{0U}; i < Count; ++i) {
        reply.push_back(buffer.at(i).first);
        reply.push_back(buffer.at(i).second);
    }
    cout<<"reply size after reserve is "<<reply.size()<<" and capacity is "<<reply.capacity()<<endl;
    
    for(auto i: reply)
        cout<<"data xx : "<<i;
    
    return 0;
}

In the above snippet i am unable to print the contents of the vector at lines

cout<<"data is "<<reply.at(0)<<reply[0]<<reply.front()<<endl;

and

for(auto i: reply)
    cout<<"data xx : "<<i;

What is the reason or error in my snippet?

ganeshredcobra
  • 1,881
  • 3
  • 28
  • 44
  • What error do you get? Include the exact error message in your question, that makes it much easier to help you understand what's wrong. – Jesper Jan 18 '22 at 11:16
  • 1
    It prints just fine for me, although you may be confused by the output, as `std::uint8_t` is usually an alias for `unsigned char` (and therefore printed as such) – UnholySheep Jan 18 '22 at 11:17
  • its not printing for me. please check this online compiler output https://godbolt.org/z/j7rGKzee6 – ganeshredcobra Jan 18 '22 at 11:37
  • @UnholySheep but in c++ we dont need any formatter's like %x or %d, cout is supposed to print any type right? – ganeshredcobra Jan 18 '22 at 11:43
  • 1
    std::cout does print just that uint8_t(5) is invisible character. Print uint8_t(33) then it will print ! – Öö Tiib Jan 18 '22 at 11:51
  • 1
    Cast the output to an integer like here : https://onlinegdb.com/zkgP0l2Hl (includes some other code review remars, and a cleaner way of initializing your array) – Pepijn Kramer Jan 18 '22 at 11:54
  • @PepijnKramer thanks for that solution and great comments – ganeshredcobra Jan 18 '22 at 12:08

0 Answers0