C++ save vector or float to bin / 字符串转数字 / 查找字符串

保存数组或vector到bin文件

void save_bin(std::vector<float> &data_vector, std::string name = "mnn.bin")
{                                                                                 
    std::ofstream outFile(name, std::ios::out | std::ios::binary);                
    int size = int(data_vector.size());                                           
    outFile.write((char *)data_vector.data(), sizeof(float) * size);              
    outFile.close();                                                              
} 
int write_bin_to_file(const char* file_path, char* buf, int size_buf )
{    
    FILE * fid = fopen(file_path ,"wb");
    for(int i = 0 ; i < size_buf; i ++)
    {
        fwrite(&buf[i],sizeof(char),1,fid);
    }
    fclose(fid);
   return 0;
} 

查找子字符串

// c++
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
 
int main() {
    string s1, s2;
    while(cin >> s1 >> s2) {
        if(s1.size() >= s2.size())
            cout << (s1.find(s2) != string::npos) << endl;
        else
            cout << (s2.find(s1) != string::npos) << endl;
    }
    return 0;
}

// c
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
 
int main()
{
    string s1, s2;
    while(cin >> s1 >> s2) {
        if(s1.size() >= s2.size())
            cout << (strstr(s1.c_str(), s2.c_str()) != nullptr) << endl;   //换行有坑!!!
        else
            cout << (strstr(s2.c_str(), s1.c_str()) != nullptr) << endl;
    }
    return 0;
}

字符串转数字

std::string s_num=node->name().substr(6,7);
int num=0;
std::stringstream strs;
strs<<s_num;
strs>>num;
tags[num]=tag_;

数字转字符串

int num = 123;
string str = to_string(num);
cout << str << endl;



#include <iostream>
#include <sstream>
using namespace std;
 
int main() {
    int num = 123;
    stringstream ss;
    ss << num;
    string str = ss.str();
    cout << str << endl;
    return 0;
}



#include <iostream>
#include <cstdio>
using namespace std;
 
int main() {
    int num = 123;
    char str[10];
    sprintf(str, "%d", num);
    cout << str << endl;
    return 0;
}


//vector插入元素加到尾部
a.emplace_back({0,1,2}); //a[0][0]=0,a[0][1]=1,a[0][2]=2;
a.push_back({1,2,3});
//插入元素,插入头部
a.insert(a.begin(),{1,1});
//插入任意位置,n是想要插入的位置
a.insert(a.begin()+n,{1,1});

posted @ 2023-08-18 11:54  WEIWEI1095  阅读(298)  评论(0)    收藏  举报
*/
作品集 //