請問 C++ 裡 string 物件的 begin() 用法
查資料是說 string::begin() 會傳回一個 iterator
但為什麼第二行都不會過呢 ?
目的: 練習將輸入的字串轉成大寫並顯示出來
1. string str = "Hellow World";
2. vector<string>::iterator it = str.begin();
錯誤 : ConsoleApplication1.cpp(16): error C2440: '正在初始化' : 無法由 'std::_String_iterator<_Mystr>' 轉換為 'std::_Vector_iterator<_Myvec>'
查了好多資料都看不懂
有人可以提供一個類似的簡單例子嗎 ?
謝謝大德
34 年前學過 Turbo C</string>
我的目的只是要練習將輸入的字串轉成大寫並顯示出來
因為聽說 C++ 並沒有將字串轉成大寫的函數可用
只好一個字一個字的轉
int _tmain(int argc, _TCHAR* argv[])
{
string str = "Hello World";
cout << str << endl;
string::iterator it = str.begin();
for (it ; it != str.end() ; it++)
*it = toupper( *it );
cout << str << endl;
system("pause");
return 0;
}
------------------------------
結果 :
Hello World
HELLO WORLD
開心

#include <iostream>
#include <string>
int main() {
std::string str("hello world!");
std::transform(str.begin(), str.end(), str.begin(),
[](char c) { return std::toupper(c);} );
std::cout << str << std::endl;
return 0;
}
確實這是個網路上有各式寫法的題目
在C++跟C差距越來越大的今日我習慣這樣寫
</string></iostream></algorithm>
內文搜尋
X



























































































