GVKun编程网logo

c – 通过迭代器进行basic_string转换是否合法?(c迭代器编写)

17

这篇文章主要围绕c–通过迭代器进行basic_string转换是否合法?和c迭代器编写展开,旨在为您提供一份详细的参考资料。我们将全面介绍c–通过迭代器进行basic_string转换是否合法?的优缺

这篇文章主要围绕c – 通过迭代器进行basic_string转换是否合法?c迭代器编写展开,旨在为您提供一份详细的参考资料。我们将全面介绍c – 通过迭代器进行basic_string转换是否合法?的优缺点,解答c迭代器编写的相关问题,同时也会为您带来.net – 在Visual Basic关键字(例如`String`)上使用CLR类型(例如`System.String`或`[String]`)是否有任何可以想象的优势?、basic_string, string, wstring、boost::asio 序列 15: basic_socket、boost::asio序列17: basic_datagram_socket 和 basic_stream_socket的实用方法。

本文目录一览:

c – 通过迭代器进行basic_string转换是否合法?(c迭代器编写)

c – 通过迭代器进行basic_string转换是否合法?(c迭代器编写)

iterator是basic_strings之间的propper转换吗?这样安全/合法吗?这会创建一个有效的字符串吗?

std::u16string str16;
//str16 is set here;
std::string cStr(str16.cbegin(),str16.cend());

在VS 2013中它似乎工作正常.

解决方法

是的,这是合法的,因为char可以从char16_t初始化.但是,如果char已签名且值太大而不适合(C11§4.7/ 3),则转换的结果是实现定义的. (char是有符号还是无符号也是实现定义的,§3.9.1/ 1.)

正如评论中所指出的,这不会将您的字符串转换为其他编码. (话虽如此,它可能适用于字符串只包含ASCII字符的情况.那就是说,不要这样做.)

.net – 在Visual Basic关键字(例如`String`)上使用CLR类型(例如`System.String`或`[String]`)是否有任何可以想象的优势?

.net – 在Visual Basic关键字(例如`String`)上使用CLR类型(例如`System.String`或`[String]`)是否有任何可以想象的优势?

我们都知道,在C#中,无论使用String(CLR类)还是字符串(C#关键字)都没有任何区别.有关详细信息,请参阅以下问题

> What is the difference between String and string in C#?

到目前为止,我的印象是VB.NET也是如此. language specification甚至说(强调我的):

The primitive types are identified through keywords,which are aliases for predefined types in the System namespace. A primitive type is completely indistinguishable from the type it aliases: writing the reserved word Byte is exactly the same as writing System.Byte.

因此,我非常惊讶地看到Visual Studio 2015有所不同:Visual Studio允许您指定框架名称(Int32 / Int64 / DateTime / …)的首选项(工具/选项/文本编辑器/基本/代码样式)原生VB关键字(整数/长/日/ …).

问题是:一旦告诉Visual Studio您更喜欢Framework名称,自动生成的代码使用[String](使用[] VB关键字转义,类似于C#的@)而不是String(对象,单一和所有相同) VB关键字与Framework类型名称匹配的其他类型).我认为这是错误的(并已提交了Connect issue),因为括号使代码混乱,并且如上所示,无论您使用[String](由于VB的自动系统导入有效地引用System.String),它都不会产生语义差异)或String(VB关键字别名System.String).

但是,由于Visual Studio开发人员非常聪明,所以我完全有可能忽略了某些东西,并且使用[String]而不是String实际上是有意义的,因此我的问题是:

在Visual Basic中使用[String]而不是String是否有任何可以想象的优势,或者Visual Studio编辑器只是“做错了什么”并且无用地混乱自动生成的代码?

解决方法

我认为使用String(指(1))会比[String]更连续.在整个地方使用[String]只是在VB的语法颜色上看起来不太好,因为它看起来像普通类型而不是关键字.我认为,当用VB语法着色时,除了它的外观之外,在代码中使用[String]而不是String是没有可能的优势.

basic_string, string, wstring

basic_string, string, wstring

string只是basic_string的宏定义,是一种特化。

我们使用vector要指定类型vector<int>,但是string不需要。因为已经指定了类型。

basic_string才是一个STL中的sequence container,

std::string类是std::basic_string模板在char类型上的一个特化。

std::wstring类是std::basic_string模板在wchar_t类型上的一个特化。

typedef basic_string<char, char_traits<char>, allocator<char> >string;

typedef basic_string<wchar_t, char_traits<wchar_t>,allocator<wchar_t> > wstring;

所以string和wstring就没有特别之处了

boost::asio 序列 15: basic_socket

boost::asio 序列 15: basic_socket

explicit basic_socket(const executor_type& ex)

创建 & 未打开的 socket

  template <typename ExecutionContext>
  explicit basic_socket(ExecutionContext& context,
      typename enable_if<
        is_convertible<ExecutionContext&, execution_context&>::value
      >::type* = 0)



创建 & 未打开的 socket
basic_socket(const executor_type& ex, const protocol_type& protocol) 创建 & 打开 socket
  template <typename ExecutionContext>
  basic_socket(ExecutionContext& context, const protocol_type& protocol,
      typename enable_if<
        is_convertible<ExecutionContext&, execution_context&>::value
      >::type* = 0)



创建 & 打开 socket
basic_socket(const executor_type& ex, const endpoint_type& endpoint) 创建 & 打开 & 绑定 local endpoint 的 socket
  template <typename ExecutionContext>
  basic_socket(ExecutionContext& context, const endpoint_type& endpoint,
      typename enable_if<
        is_convertible<ExecutionContext&, execution_context&>::value
      >::type* = 0)



创建 & 打开 & 绑定 local endpoint 的 socket
  basic_socket(const executor_type& ex, const protocol_type& protocol,
      const native_handle_type& native_socket)
对已经创建的原生套接字创建 socket
  template <typename ExecutionContext>
  basic_socket(ExecutionContext& context, const protocol_type& protocol,
      const native_handle_type& native_socket,
      typename enable_if<
        is_convertible<ExecutionContext&, execution_context&>::value
      >::type* = 0)




对已经创建的原生套接字创建 socket
basic_socket(basic_socket&& other) 从一个 socket 移动构造到另一个 socket
basic_socket& operator=(basic_socket&& other) 从一个 socket 移动构造到另一个 socket
  template <typename Protocol1, typename Executor1>
  basic_socket(basic_socket<Protocol1, Executor1>&& other,
      typename enable_if<
        is_convertible<Protocol1, Protocol>::value
          && is_convertible<Executor1, Executor>::value
      >::type* = 0)




从一个 socket 移动构造到另一个 socket
  template <typename Protocol1, typename Executor1>
  typename enable_if<
    is_convertible<Protocol1, Protocol>::value
      && is_convertible<Executor1, Executor>::value,
    basic_socket&
  >::type operator=(basic_socket<Protocol1, Executor1> && other)




从一个 socket 移动构造到另一个 socket
executor_type get_executor() 获取执行器
lowest_layer_type& lowest_layer() 获取 basic_socket 对象的引用
void open(const protocol_type& protocol = protocol_type()) 打开指定协议的 socket
  BOOST_ASIO_SYNC_OP_VOID open(const protocol_type& protocol,
      boost::system::error_code& ec)
打开指定协议的 socket & 返回错误信息

  void assign(const protocol_type& protocol,
      const native_handle_type& native_socket)

  BOOST_ASIO_SYNC_OP_VOID assign(const protocol_type& protocol,
      const native_handle_type& native_socket, boost::system::error_code& ec)

创建 & 打开 socket 控制原生 socket
bool is_open() 判断 socket 是否打开

void close()

BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)

关闭 socket

native_handle_type release()

native_handle_type release(boost::system::error_code& ec)

释放对原生 socket 的控制
 native_handle_type native_handle() 获取原生 socket

void cancel()

BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)

取消异步操作

bool at_mark()

bool at_mark(boost::system::error_code& ec)

确定套接字是否位于 out-of-band 数据标记

std::size_t available()

std::size_t available(boost::system::error_code& ec)

获取 socket 可读数据量

void bind(const endpoint_type& endpoint)

  BOOST_ASIO_SYNC_OP_VOID bind(const endpoint_type& endpoint,
      boost::system::error_code& ec)

绑定 socket 道 local endpoint

void connect(const endpoint_type& peer_endpoint)

  BOOST_ASIO_SYNC_OP_VOID connect(const endpoint_type& peer_endpoint,
      boost::system::error_code& ec)

同步阻塞式 socket 连接到指定的 peer_endpoint
  async_connect(const endpoint_type& peer_endpoint,
      BOOST_ASIO_MOVE_ARG(ConnectHandler) handler)
异步非阻塞 socket 连接到指定的 peer_endpoint

  template <typename SettableSocketOption>
  void set_option(const SettableSocketOption& option)

  template <typename SettableSocketOption>
  BOOST_ASIO_SYNC_OP_VOID set_option(const SettableSocketOption& option,
      boost::system::error_code& ec)

   * boost::asio::socket_base::broadcast @n
   * boost::asio::socket_base::do_not_route @n
   * boost::asio::socket_base::keep_alive @n
   * boost::asio::socket_base::linger @n
   * boost::asio::socket_base::receive_buffer_size @n
   * boost::asio::socket_base::receive_low_watermark @n
   * boost::asio::socket_base::reuse_address @n
   * boost::asio::socket_base::send_buffer_size @n
   * boost::asio::socket_base::send_low_watermark @n
   * boost::asio::ip::multicast::join_group @n
   * boost::asio::ip::multicast::leave_group @n
   * boost::asio::ip::multicast::enable_loopback @n
   * boost::asio::ip::multicast::outbound_interface @n
   * boost::asio::ip::multicast::hops @n
   * boost::asio::ip::tcp::no_delay













  template <typename GettableSocketOption>
  void get_option(GettableSocketOption& option)

  template <typename GettableSocketOption>
  BOOST_ASIO_SYNC_OP_VOID get_option(GettableSocketOption& option,
      boost::system::error_code& ec)

同上

  template <typename IoControlCommand>
  void io_control(IoControlCommand& command)

  template <typename IoControlCommand>
  BOOST_ASIO_SYNC_OP_VOID io_control(IoControlCommand& command,
      boost::system::error_code& ec)

执行 IO Commond

bool non_blocking()

  BOOST_ASIO_SYNC_OP_VOID non_blocking(
      bool mode, boost::system::error_code& ec)

void non_blocking(bool mode)

(1) mode = true 时,对于不能马上执行同步操作时,将导致失败 boost::asio::error::would_block

(2) mode = false 时,同步操作将阻塞直到完成

bool native_non_blocking()

  BOOST_ASIO_SYNC_OP_VOID native_non_blocking(
      bool mode, boost::system::error_code& ec)

void native_non_blocking(bool mode)

 

同上,但是对应原生套接字

endpoint_type local_endpoint()

endpoint_type local_endpoint(boost::system::error_code& ec)

获取本地 end_point

endpoint_type remote_endpoint()

endpoint_type remote_endpoint(boost::system::error_code& ec)

获取对端的 end_point

void shutdown(shutdown_type what)

 BOOST_ASIO_SYNC_OP_VOID shutdown(shutdown_type what,
      boost::system::error_code& ec)

关闭 socket

void wait(wait_type w)

BOOST_ASIO_SYNC_OP_VOID wait(wait_type w, boost::system::error_code& ec)

阻塞式等待 socket 进入 wait_type 指定的的就绪状态 (读就绪,写就绪,错误就绪)
  template <typename WaitHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
      void (boost::system::error_code))
  async_wait(wait_type w, BOOST_ASIO_MOVE_ARG(WaitHandler) handler)


异步等待 socket 进入 wait_type 指定的就绪状态 (读就绪,写就绪,错误就绪)

 

  • 点赞
  • 收藏
  • 分享
    • 文章举报
Hit_HSW
发布了 203 篇原创文章 · 获赞 109 · 访问量 32 万 +
私信 关注

boost::asio序列17: basic_datagram_socket 和 basic_stream_socket

boost::asio序列17: basic_datagram_socket 和 basic_stream_socket

  explicit basic_datagram_socket(const executor_type& ex)

  template <typename ExecutionContext>
  explicit basic_datagram_socket(ExecutionContext& context,
      typename enable_if<
        is_convertible<ExecutionContext&, execution_context&>::value
      >::type* = 0)



创建&未打开udp socket

  basic_datagram_socket(const executor_type& ex, const protocol_type& protocol)

  template <typename ExecutionContext>
  basic_datagram_socket(ExecutionContext& context,
      const protocol_type& protocol,
      typename enable_if<
        is_convertible<ExecutionContext&, execution_context&>::value
      >::type* = 0)




创建&打开 udp socket

basic_datagram_socket(const executor_type& ex, const endpoint_type& endpoint)

  template <typename ExecutionContext>
  basic_datagram_socket(ExecutionContext& context,
      const endpoint_type& endpoint,
      typename enable_if<
        is_convertible<ExecutionContext&, execution_context&>::value
      >::type* = 0)




创建&打开&绑定到自定local end_point的udp socket

  basic_datagram_socket(const executor_type& ex,
      const protocol_type& protocol, const native_handle_type& native_socket)

  template <typename ExecutionContext>
  basic_datagram_socket(ExecutionContext& context,
      const protocol_type& protocol, const native_handle_type& native_socket,
      typename enable_if<
        is_convertible<ExecutionContext&, execution_context&>::value
      >::type* = 0)




由原生套接字创建udp socket

  basic_datagram_socket(basic_datagram_socket&& other)

basic_datagram_socket& operator=(basic_datagram_socket&& other)

  template <typename Protocol1, typename Executor1>
  basic_datagram_socket(basic_datagram_socket<Protocol1, Executor1>&& other,
      typename enable_if<
        is_convertible<Protocol1, Protocol>::value
          && is_convertible<Executor1, Executor>::value
      >::type* = 0)




  template <typename Protocol1, typename Executor1>
  typename enable_if<
    is_convertible<Protocol1, Protocol>::value
      && is_convertible<Executor1, Executor>::value,
    basic_datagram_socket&
  >::type operator=(basic_datagram_socket<Protocol1, Executor1>&& other)




移动构造函数
  template <typename ConstBufferSequence>
  std::size_t send(const ConstBufferSequence& buffers)
用于stream socket阻塞式发送数据,buffers为发送的数据

  template <typename ConstBufferSequence>
  std::size_t send(const ConstBufferSequence& buffers,
      socket_base::message_flags flags)

  template <typename ConstBufferSequence>
  std::size_t send(const ConstBufferSequence& buffers,
      socket_base::message_flags flags, boost::system::error_code& ec)

 用于stream socket阻塞式发送数据,buffers为发送的数据,其中message_flags可取的值:

BOOST_ASIO_STATIC_CONSTANT(int,
      message_peek = BOOST_ASIO_OS_DEF(MSG_PEEK));
  BOOST_ASIO_STATIC_CONSTANT(int,
      message_out_of_band = BOOST_ASIO_OS_DEF(MSG_OOB));
  BOOST_ASIO_STATIC_CONSTANT(int,
      message_do_not_route = BOOST_ASIO_OS_DEF(MSG_DONTROUTE));
  BOOST_ASIO_STATIC_CONSTANT(int,
      message_end_of_record = BOOST_ASIO_OS_DEF(MSG_EOR));






  template <typename ConstBufferSequence, typename WriteHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
      void (boost::system::error_code, std::size_t))
  async_send(const ConstBufferSequence& buffers,
      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)



  template <typename ConstBufferSequence, typename WriteHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
      void (boost::system::error_code, std::size_t))
  async_send(const ConstBufferSequence& buffers,
      socket_base::message_flags flags,
      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)




用于stream socket,异步发送数据,

  template <typename ConstBufferSequence>
  std::size_t send_to(const ConstBufferSequence& buffers,
      const endpoint_type& destination)

  template <typename ConstBufferSequence>
  std::size_t send_to(const ConstBufferSequence& buffers,
      const endpoint_type& destination, socket_base::message_flags flags)

  template <typename ConstBufferSequence>
  std::size_t send_to(const ConstBufferSequence& buffers,
      const endpoint_type& destination, socket_base::message_flags flags,
      boost::system::error_code& ec)


用于datagram socket,阻塞式发送数据

  template <typename ConstBufferSequence, typename WriteHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
      void (boost::system::error_code, std::size_t))
  async_send_to(const ConstBufferSequence& buffers,
      const endpoint_type& destination,
      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  {
    return async_initiate<WriteHandler,
      void (boost::system::error_code, std::size_t)>(
        initiate_async_send_to(), handler, this, buffers,
        destination, socket_base::message_flags(0));
  }










  template <typename ConstBufferSequence, typename WriteHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
      void (boost::system::error_code, std::size_t))
  async_send_to(const ConstBufferSequence& buffers,
      const endpoint_type& destination, socket_base::message_flags flags,
      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)




用于datagram socket,非阻塞式发送数据

  template <typename MutableBufferSequence>
  std::size_t receive(const MutableBufferSequence& buffers)

  template <typename MutableBufferSequence>
  std::size_t receive(const MutableBufferSequence& buffers,
      socket_base::message_flags flags)

  template <typename MutableBufferSequence>
  std::size_t receive(const MutableBufferSequence& buffers,
      socket_base::message_flags flags, boost::system::error_code& ec)

用于stream socket,阻塞式接收数据

  template <typename MutableBufferSequence, typename ReadHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
      void (boost::system::error_code, std::size_t))
  async_receive(const MutableBufferSequence& buffers,
      BOOST_ASIO_MOVE_ARG(ReadHandler) handler)



  template <typename MutableBufferSequence, typename ReadHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
      void (boost::system::error_code, std::size_t))
  async_receive(const MutableBufferSequence& buffers,
      socket_base::message_flags flags,
      BOOST_ASIO_MOVE_ARG(ReadHandler) handler)




用于stream socket,非阻塞式接收数据

  template <typename MutableBufferSequence>
  std::size_t receive_from(const MutableBufferSequence& buffers,
      endpoint_type& sender_endpoint)

  template <typename MutableBufferSequence>
  std::size_t receive_from(const MutableBufferSequence& buffers,
      endpoint_type& sender_endpoint, socket_base::message_flags flags)

  template <typename MutableBufferSequence>
  std::size_t receive_from(const MutableBufferSequence& buffers,
      endpoint_type& sender_endpoint, socket_base::message_flags flags,
      boost::system::error_code& ec)


用于datagram socket,阻塞式接收数据,对端end_point保存在sender_endpoint中

  template <typename MutableBufferSequence, typename ReadHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
      void (boost::system::error_code, std::size_t))
  async_receive_from(const MutableBufferSequence& buffers,
      endpoint_type& sender_endpoint,
      BOOST_ASIO_MOVE_ARG(ReadHandler) handler)




  template <typename MutableBufferSequence, typename ReadHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
      void (boost::system::error_code, std::size_t))
  async_receive_from(const MutableBufferSequence& buffers,
      endpoint_type& sender_endpoint, socket_base::message_flags flags,
      BOOST_ASIO_MOVE_ARG(ReadHandler) handler)




用于datagram socket, 非阻塞式接收数据,对端end_point保存在sender_endpoint中

对于basic_stream_socket 和 basic_datagram_socket类似,调用方式和参数相同,不同之处在于basic_datagram_socket只能调用datagram socket相关的函数,basic_stream_socket只能调用stream socket相关的函数

  • 点赞
  • 收藏
  • 分享
    • 文章举报
Hit_HSW
发布了203 篇原创文章 · 获赞 109 · 访问量 32万+
私信 关注

今天的关于c – 通过迭代器进行basic_string转换是否合法?c迭代器编写的分享已经结束,谢谢您的关注,如果想了解更多关于.net – 在Visual Basic关键字(例如`String`)上使用CLR类型(例如`System.String`或`[String]`)是否有任何可以想象的优势?、basic_string, string, wstring、boost::asio 序列 15: basic_socket、boost::asio序列17: basic_datagram_socket 和 basic_stream_socket的相关知识,请在本站进行查询。

本文标签: