本人小白,只知道cout用于输出,语法格式为cout << "hello world ",但是对于如下:
ss << "hello world " << count
的如何理解,请指点一下,谢谢
count,不是cout
另外ss是什么,可能是别的流对象吧,比如文件流、异常流、网络流等等。
这种写法没有奇怪的。
C++语句:
ss << "hello world " << count
一般出现在创建ROS话题的发布者(Publisher)节点程序中,是利用c++自带的头文件sstream,来实现利用输入输出流的方式往string里写东西,并且还可以拼接string和其他类型的变量。
该语句实现了string型的数据 hello world和int型变量 count的拼接,形成一个新的string。即如果count是1,那么hello world1会作为string被存放在ss当中。
怎么调用这个string呢? ss.str()就可以了。最后可以用ROS_INFO输出。
附上:ROS话题发布者节点程序代码
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
/**
* This tutorial demonstrates simple sending of messages over the ROS system.
*/
int main(int argc, char **argv)
{
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
int count = 0;
while (ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
这个是ROS下用C++语言写的,这里面的count是前面定义的int型数据,其实这句话就是吧hello world 加上count的值一起给ss,最后可以用ROS_INFO输出
吓得我赶紧编译测试了一下,事实证明,编译器也不知道这是啥
是我头文件没加还是咋地?
我觉着如果你的程序真的没报错而且输出的话可能是在哪里定义了ss的操作是输出的意思,就像cout也只是因为我们给cout定义了它具有输出的功能他才成为关键字而已。
又或者我孤陋寡闻了没见过这种,求知情大神指教