常用字符类
##使用方法如下
#include <QtCore/QCoreApplication>
#include <iostream>
#include <string>
class Head {
public:
int channel, type;
friend ::std::ostream& operator << (::std::ostream& os, const Head& head) {
return os << head.channel << " " << head.type;
}
};
class Msg {
public:
Head head;
char arr[256];
friend ::std::ostream& operator <<(::std::ostream& os, const Msg& msg) {
return os << msg.head.channel << " " << msg.head.type << " " << '\n' << msg.arr;
}
};
class PeerMsg {
public:
int ipv4, port;
explicit PeerMsg(const int _ipv4, const int _port) :ipv4(_ipv4), port(_port) {}
friend ::std::ostream& operator <<(::std::ostream& os, const PeerMsg& peermsg) {
return os << peermsg.ipv4 << " " << peermsg.port;
}
};
class Demo
{
public:
inline explicit Demo() {}
inline ~Demo(){}
void demo_01();
void demo_02();
void demo_03();
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Demo test;
//test.demo_01();
//test.demo_02();
test.demo_03();
// return a.exec();
return 0;
}
void Demo::demo_01()
{
//"QtGlobal"中的一些基本数据类型
//VC "qglobal"
::std::cout << "sizeof(uchar)=" << sizeof(uchar) << ::std::endl;
::std::cout << "sizeof(quint8)=" << sizeof(quint8) << ::std::endl;
::std::cout << "sizeof(qint16)=" << sizeof(qint16) << ::std::endl;
::std::cout << "sizeof(ushort)=" << sizeof(ushort) << ::std::endl;
}
void Demo::demo_02()
{
//QByteArray
/*
if defined(QT_BUILD_CORE_LIB)
# define Q_CORE_EXPORT Q_DECL_EXPORT
# else
# define Q_CORE_EXPORT Q_DECL_IMPORT
# endif
class Q_CORE_EXPORT QByteArray
*/
/* inline QByteArray() Q_DECL_NOTHROW;
QByteArray(const char *, int size = -1);
QByteArray(int size, char c);
QByteArray(int size, Qt::Initialization);
inline QByteArray(const QByteArray &) Q_DECL_NOTHROW;
inline ~QByteArray();
QByteArray &operator=(const QByteArray &) Q_DECL_NOTHROW;
QByteArray &operator=(const char *str);
*/
//# define Q_DECL_NOTHROW Q_DECL_NOEXCEPT
const char* ch = "";
::std::cout << "const char* ch= " << typeid(ch).name() << ::std::endl;
QByteArray arr("milk apple", ::std::string("milk").size());
::std::cout << "arr=" << arr.toStdString() << ::std::endl;
arr = "apple";//const char* =QByteArr(const char*,int size=-1);
::std::cout << "arr=" << arr.toStdString() << ::std::endl;
if (arr[(arr.size())]=='\0')::std::cout << "arr.at(arr.size())" << ::std::endl;
//arr.at(arr.size()) error
//QString
/*
Q_DECL_CONSTEXPR QLatin1String mid(int pos) const
{ return Q_ASSERT(pos >= 0), Q_ASSERT(pos <= size()), QLatin1String(m_data + pos, m_size - pos); }
Q_DECL_CONSTEXPR QLatin1String mid(int pos, int n) const
{ return Q_ASSERT(pos >= 0), Q_ASSERT(n >= 0), Q_ASSERT(pos + n <= size()), QLatin1String(m_data + pos, n); }
Q_DECL_CONSTEXPR QLatin1String left(int n) const
{ return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QLatin1String(m_data, n); }
Q_DECL_CONSTEXPR QLatin1String right(int n) const
{ return Q_ASSERT(n >= 0), Q_ASSERT(n <= size()), QLatin1String(m_data + m_size - n, n); }
*/
//# define Q_DECL_CONSTEXPR constexpr
::std::cout << arr.left(arr.size()).left(1).toStdString() << ::std::endl;
/*
#define nodiscard EMIT WARNING C4005 # define Q_REQUIRED_RESULT [[nodiscard]]
Q_REQUIRED_RESULT static QByteArray number(int, int base = 10);
Q_REQUIRED_RESULT static QByteArray number(uint, int base = 10);
Q_REQUIRED_RESULT static QByteArray number(qlonglong, int base = 10);
Q_REQUIRED_RESULT static QByteArray number(qulonglong, int base = 10);
Q_REQUIRED_RESULT static QByteArray number(double, char f = 'g', int prec = 6);
Q_REQUIRED_RESULT static QByteArray fromRawData(const char *, int size);
Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64, Base64Options options);
Q_REQUIRED_RESULT static QByteArray fromBase64(const QByteArray &base64); // ### Qt6 merge with previous
Q_REQUIRED_RESULT static QByteArray fromHex(const QByteArray &hexEncoded);
Q_REQUIRED_RESULT static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent = '%');
*/
::std::cout << QByteArray::fromHex("5174").toStdString() << ::std::endl;
::std::cout << QByteArray::number(63, 16).toUpper().toStdString() << ::std::endl;
/*
Q_REQUIRED_RESULT QByteArray toLower() const &
{ return toLower_helper(*this); }
Q_REQUIRED_RESULT QByteArray toLower() &&
{ return toLower_helper(*this); }
Q_REQUIRED_RESULT QByteArray toUpper() const &
{ return toUpper_helper(*this); }
Q_REQUIRED_RESULT QByteArray toUpper() &&
{ return toUpper_helper(*this); }
*/
::std::cout << "*************************************" << ::std::endl;
//QByteArray and class/struct
Msg msg;
msg.head.channel = 1001; msg.head.type = 1;
::std::strcpy(msg.arr, "abcdef");
::std::cout << msg << ::std::endl;
arr.clear();
// QByteArray &append(const char *s);
//QByteArray& append(const char* s, int len);
//QByteArray& append(const QByteArray & a);
arr.append((char*)&msg, sizeof(msg));
::std::cout << *(Msg*)arr.data() << ::std::endl;
PeerMsg peermsg(123456, 10086);
arr.clear();
arr.append((char*)&peermsg, sizeof(peermsg));
arr.append((char*)&msg, sizeof(msg));
::std::cout << *(PeerMsg*)arr.data() << ::std::endl;
::std::cout << *(Msg*)(arr.right(arr.size() - sizeof(*(PeerMsg*)arr.data()))).data() << ::std::endl;
}
void Demo::demo_03()
{
//QString
/*
Q_DECL_NOTHROW = noexcept
inline QString() Q_DECL_NOTHROW;
explicit QString(const QChar *unicode, int size = -1);
QString(QChar c);
QString(int size, QChar c);
inline QString(QLatin1String latin1);
inline QString(const QString &) Q_DECL_NOTHROW;
*/
QString qstr;
::std::string str = qstr.toStdString();
qstr.append("milk and apple");
::std::cout << "qstr: " << qstr.toStdString() << ::std::endl;
static const QChar qarr[5]{ ' ','A','B','C','D' };
qstr.append(qarr, 5);
std::cout << qstr.sprintf("%s %.1f", "float:", 100.0).toStdString() << ::std::endl;
::std::cout << "qstr: " << qstr.toStdString() << ::std::endl;
qstr.clear();
qstr = QString("%1 int %2 float").arg(100).arg(100.0);
::std::cout << qstr.toStdString() << ::std::endl;
::std::cout << QString::number(121313).toStdString() << ::std::endl;
}