這個類型很常用,但是我又記不住用法,用一次查一次…幹脆總結一下自己看着友善。
簡介
C++中經常遇到各種字元串之間轉化的問題,比如最常見的int轉成string。雖然有的好像有直接的轉換函數,但是使用std::stringstream類型的字元串進行轉換基本上可以應對是以常見字元串的轉換。
首先他的頭檔案是:sstream
代碼示意:
//例程:讀取檔案夾中所有圖檔
#include <iostream>
#include <string.h>
#include <sstream>//stringstream的頭檔案
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
std::stringstream SStr;
string tem;
//檔案夾路徑
string Input = "D://image//";
//讀取圖檔
for (int Num = 0; Num <= 10; Num++)
{
SStr.clear();/*std::stringstream變量在使用(轉化)前都應先調用.cleat()函數*/
//将int類轉換為string類
SStr << Num;
SStr >> tem;
//以上兩步就是将int類型的Num指派給string類型的tem(類型改變但是内容不變),然後就可以對其進行string類型的操作了。
string output = Input + tem + ".jpg";
Mat image = imread(output);
tem.clear();
}
//system("pause");
return 0;
}
當然,如果隻是單純的int轉String,有個更簡單的方法:
//例程:讀取檔案夾中所有圖檔
#include <iostream>
#include <string.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
//檔案夾路徑
string Input = "D://image//";
//讀取圖檔
for (int Num = 0; Num <= 10; Num++)
{
//直接使用to_string()函數
string output = Input + to_string(Num) + ".jpg";
Mat image = imread(output);
tem.clear();
}
//system("pause");
return 0;
}
我這裡沒有圖檔,就放一個輸出的檔案路徑示意一下就是了,每一行就是一個string類型的“output”,路徑正确的話可以使用
Mat image = imread(output);
直接讀圖檔。