c++输出打印string类型
时间: 2023-07-19 21:17:39 浏览: 56
可以使用cout来输出打印string类型,例如:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello, World!";
cout << str << endl;
return 0;
}
```
输出结果为:Hello, World!
相关问题
c++打印string类型字符串
要打印string类型字符串,可以使用printf或cout语句。下面是使用printf打印string类型字符串的示例:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello, world!";
printf("%s\n", str);
return 0;
}
```
输出:
```
Hello, world!
```
另外,如果使用C++,可以使用cout语句来打印string类型字符串,示例如下:
```c++
#include <iostream>
#include <string>
int main()
{
std::string str = "Hello, world!";
std::cout << str << std::endl;
return 0;
}
```
输出:
```
Hello, world!
```
c++ ros自定义消息中string类型数据如何打印输出
在ROS中,可以使用rospy.loginfo()函数打印输出自定义消息中的string类型数据。
例如,假设我们有一个自定义消息类型MyMsg,其中包含一个string类型的数据my_string,可以通过以下方式在ROS节点中打印输出该数据:
```
import rospy
from my_package.msg import MyMsg
def callback(data):
rospy.loginfo("Received string: %s", data.my_string)
rospy.init_node('my_node')
rospy.Subscriber('my_topic', MyMsg, callback)
rospy.spin()
```
在上面的例子中,我们定义了一个回调函数callback,它接收一个MyMsg类型的数据。在回调函数中,我们使用rospy.loginfo()函数打印输出my_string数据。最后,我们使用rospy.spin()函数让节点进入循环等待状态,以接收消息。
当接收到消息时,会在控制台输出类似以下的信息:
```
[INFO] [1616179368.876809]: Received string: Hello ROS!
```
其中,Hello ROS!是自定义消息中的my_string数据。