C++基础---string类的data/c_str/copy

1.string类的data/c_str/copy

1.1 std::string::data

  • 原型: const char *data()const;
  • 功能: string型字符串转化为char型字符串。
  • 说明:将自身转化为一个以空终止符结束(即带”\0“结尾)的char型字符串。
  • 返回值:一个指向字符串数组的指针,字符串数组指针。
  • 代码示例:

    (1)示例代码一:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str = "Test string";
        char *cstr = "Test string";
    
        if (str.length() == strlen(cstr))
        {
            cout<<"str and cstr have the same length."<<endl;
    
            if (memcmp(cstr, str.data(), str.length()) == 0)
            {
                cout<<"str and cstr have the same content."<<endl;
            }
        }
    
        system("pause");
        return 0;
    }
    =>str and cstr have the same length.
      str and cstr have the same content.
    (2)示例代码二:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str("Please split this sentence into tokens");
        const char *split = " ";
        char *cstr = new char [str.length()+1];
        strcpy_s(cstr, str.size() + 1, str.data());
    
        //cstr now contains a c-string copy of str
        char *p = 0;
        char *pNext = 0;
        p = strtok_s(cstr, split, &pNext);
        while(p != 0)
        {
            cout<<p<<endl;
            p = strtok_s(NULL, split, &pNext);
        }
    
        delete[] cstr;
        system("pause");
    
        return 0;
    }   
    =>Please
      split
      this
      sentence
      into
      tokens

1.2 std::string::c_str

  • 原型:const char *c_str()const;
  • 功能: string型字符串转化为char型字符串。
  • 说明:将自身转化为一个以空终止符结束(即带”\0“结尾)的char型字符串。
  • 返回值:一个指向字符串数组的指针,字符串数组指针。
  • 代码示例:

    (1)示例代码一:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str = "Test string";
        char *cstr = "Test string";
    
        if (str.length() == strlen(cstr))
        {
            cout<<"str and cstr have the same length."<<endl;
    
            if (memcmp(cstr, str.c_str(), str.length()) == 0)
            {
                cout<<"str and cstr have the same content"<<endl;
            }
        }
    
        system("pause");
        return 0;
    }
    =>str and cstr have the same length.
      str and cstr have the same content.
    (2)示例代码二:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str("Please split this sentence into tokens");
        const char *split = " ";
        char *cstr = new char [str.length()+1];
        strcpy_s(cstr, str.size() + 1, str.c_str());
    
        //cstr now contains a c-string copy of str
        char *p = 0;
        char *pNext = 0;
        p = strtok_s(cstr, split, &pNext);
        while (p != 0)
        {
            cout<<p<<endl;
            p = strtok_s(NULL, split, &pNext);
        }
    
        delete[] cstr;
        system("pause");
    
        return 0;
    }
    =>Please
      split
      this
      sentence
      into
      tokens

1.3 std::string::copy

  • 原型:size_t copy (char* s, size_t len, size_t pos = 0) const;
  • 功能: string型字符串转化为char型字符串。
  • 说明:从源字符串以下标为pos(默认为0)处开始拷贝n个字符到char型字符串s中。
  • 返回值:实际拷贝的字符个数。
  • 代码示例:

    
    #pragma warning(disable:4996) //全部关掉
    
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        char buffer[20];
        string str("Test string...");
        size_t length = str.copy(buffer, 6, 5);
        buffer[length]='\0';
        cout<<"buffer contains: "<<buffer<<endl;
        system("pause");
        return 0;
    }
    =>buffer contains: string

参考文献:
[1] 网络资源: http://www.cplusplus.com/reference/string/string/data/
       http://www.cplusplus.com/reference/string/string/c_str/
       http://www.cplusplus.com/reference/string/string/copy/

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用FFmpeg库来保存视频文件,具体实现可以参考以下代码: ```c++ #include <iostream> #include <string> #include <sstream> #include <fstream> #include <chrono> #include <thread> #include <opencv2/opencv.hpp> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> using namespace std; using namespace cv; int main(int argc, char* argv[]) { // 初始化FFmpeg库 av_register_all(); avcodec_register_all(); // 打开视频文件 string filename = "test.mp4"; AVFormatContext* format_ctx = nullptr; if (avformat_open_input(&format_ctx, filename.c_str(), nullptr, nullptr) != 0) { cerr << "Failed to open video file " << filename << endl; return -1; } // 查找视频流 int video_stream_index = -1; for (unsigned int i = 0; i < format_ctx->nb_streams; i++) { if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; break; } } if (video_stream_index == -1) { cerr << "Failed to find video stream in " << filename << endl; avformat_close_input(&format_ctx); return -1; } // 获取视频流的解码器 AVCodec* codec = avcodec_find_decoder(format_ctx->streams[video_stream_index]->codecpar->codec_id); if (codec == nullptr) { cerr << "Failed to find codec for video stream in " << filename << endl; avformat_close_input(&format_ctx); return -1; } // 打开解码器 AVCodecContext* codec_ctx = avcodec_alloc_context3(codec); if (avcodec_parameters_to_context(codec_ctx, format_ctx->streams[video_stream_index]->codecpar) != 0) { cerr << "Failed to copy codec parameters to decoder context in " << filename << endl; avcodec_free_context(&codec_ctx); avformat_close_input(&format_ctx); return -1; } if (avcodec_open2(codec_ctx, codec, nullptr) != 0) { cerr << "Failed to open codec for video stream in " << filename << endl; avcodec_free_context(&codec_ctx); avformat_close_input(&format_ctx); return -1; } // 创建视频文件 string output_filename = "output.mp4"; AVFormatContext* output_format_ctx = nullptr; if (avformat_alloc_output_context2(&output_format_ctx, nullptr, nullptr, output_filename.c_str()) != 0) { cerr << "Failed to create output format context for " << output_filename << endl; avcodec_free_context(&codec_ctx); avformat_close_input(&format_ctx); return -1; } // 添加视频流 AVStream* output_stream = avformat_new_stream(output_format_ctx, nullptr); if (output_stream == nullptr) { cerr << "Failed to create output video stream for " << output_filename << endl; avcodec_free_context(&codec_ctx); avformat_close_input(&format_ctx); avformat_free_context(output_format_ctx); return -1; } output_stream->id = output_format_ctx->nb_streams - 1; if (avcodec_parameters_copy(output_stream->codecpar, format_ctx->streams[video_stream_index]->codecpar) != 0) { cerr << "Failed to copy codec parameters to output video stream in " << output_filename << endl; avcodec_free_context(&codec_ctx); avformat_close_input(&format_ctx); avformat_free_context(output_format_ctx); return -1; } output_stream->codecpar->codec_tag = 0; if (avformat_write_header(output_format_ctx, nullptr) != 0) { cerr << "Failed to write output file header for " << output_filename << endl; avcodec_free_context(&codec_ctx); avformat_close_input(&format_ctx); avformat_free_context(output_format_ctx); return -1; } // 分配解码缓存 AVFrame* frame = av_frame_alloc(); AVPacket packet; av_init_packet(&packet); // 读取视频帧并保存 while (av_read_frame(format_ctx, &packet) == 0) { if (packet.stream_index == video_stream_index) { if (avcodec_send_packet(codec_ctx, &packet) != 0) { cerr << "Failed to send packet to decoder in " << filename << endl; break; } while (avcodec_receive_frame(codec_ctx, frame) == 0) { // 将YUV图像转换为BGR图像 Mat bgr_image(frame->height, frame->width, CV_8UC3); SwsContext* sws_ctx = sws_getContext(frame->width, frame->height, (AVPixelFormat)frame->format, frame->width, frame->height, AV_PIX_FMT_BGR24, SWS_BILINEAR, nullptr, nullptr, nullptr); uint8_t* data[AV_NUM_DATA_POINTERS] = { 0 }; data[0] = bgr_image.data; int linesize[AV_NUM_DATA_POINTERS] = { 0 }; linesize[0] = bgr_image.step; sws_scale(sws_ctx, frame->data, frame->linesize, 0, frame->height, data, linesize); sws_freeContext(sws_ctx); // 保存BGR图像 AVPacket output_packet; av_init_packet(&output_packet); output_packet.data = nullptr; output_packet.size = 0; avcodec_send_frame(codec_ctx, frame); while (avcodec_receive_packet(codec_ctx, &output_packet) == 0) { output_packet.stream_index = output_stream->id; av_write_frame(output_format_ctx, &output_packet); av_packet_unref(&output_packet); } // 显示BGR图像 imshow("BGR Image", bgr_image); waitKey(1); } } av_packet_unref(&packet); } // 释放资源 av_write_trailer(output_format_ctx); avcodec_free_context(&codec_ctx); avformat_close_input(&format_ctx); avformat_free_context(output_format_ctx); av_frame_free(&frame); return 0; } ``` 这段代码可以读取一个视频文件,将其中的每一帧转换为BGR格式的图像,并保存为另一个视频文件。其中,使用了OpenCV库来显示BGR图像,使用了FFmpeg库来读取和保存视频文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值