0
A
回答
1
string org = "12344//1238";
size_t p = org.find("//");
string str2 = org.substr(0,p);
string str3 = org.substr(p+2,org.size());
cout << str2 << " "<< str3;
0
0
這應該分割和轉換成整數請看:
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
class BadConversion : public std::runtime_error {
public:
BadConversion(std::string const& s)
: std::runtime_error(s)
{ }
};
inline double convertToInt(std::string const& s,
bool failIfLeftoverChars = true)
{
std::istringstream i(s);
int x;
char c;
if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
throw BadConversion("convertToInt(\"" + s + "\")");
return x;
}
int main()
{
std::string pieces = "12344//1238";
unsigned pos;
pos = pieces.find("//");
std::string first = pieces.substr(0, pos);
std::string second = pieces.substr(pos + 2);
std::cout << "first: " << first << " second " << second << std::endl;
double d1 = convertToInt(first), d2 = convertToInt(second) ;
std::cout << d1 << " " << d2 << std::endl ;
}
+0
這是什麼? – Madbreaks 2013-04-26 18:29:37
0
我能想到的最簡單的方法:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
void main()
{
int int1, int2;
char slash1, slash2;
//HERE IT IS:
stringstream os ("12344//1238");
os>> int1 >> slash1 >> slash2 >> int2;
//You may want to verify that slash1 and slash2 really are /'s
cout << "I just read in " << int1 << " and " << int2 << ".\n";
system ("pause");
}
也很好,因爲它很容易重寫 - 例如,如果你決定閱讀由其他東西分隔的整數。
1
0
取整數,作爲一個字符串。 該字符串將會有數字和//符號。 接下來,您可以運行一個簡單的for循環來查找字符串中的'/'。 符號之前的值存儲在另一個字符串中。 當出現'/'時,for循環將終止。您現在有第一個 '/'符號的索引。 遞增索引並在另一個 字符串中使用forothe循環複製字符串的其餘部分。 現在你有兩個單獨的字符串。
相關問題
- 1. 使用多字符字符串的拆分字符串
- 2. 如何使用字符串分隔符分割字符串?
- 3. 如何使用字符串分隔符分割字符串?
- 4. 使用字符串作爲分隔符來拆分字符串
- 5. 使用字符串分割
- 6. 使用'|'分割字符串
- 7. 用字符串拆分字符串?
- 8. 用C拆分字符串
- 9. 分割使用boost ::字符串分割
- 10. 如何使用分隔字符串分割字符串「#|#」
- 11. 區分字符串中使用字符串分割
- 12. 分割字符串的字符C#
- 13. Elisp拆分字符串函數來拆分字符串。字符
- 14. C#拆分字符串 - 將字符串拆分爲數組
- 15. 使用C++拆分字符串11
- 16. 字符串分割到字符串
- 17. Ç - 字符串分割成字符串
- 18. 字符串分割到字符串
- 19. 從字符串拆分字符串
- 20. 將字符串拆分爲字符串
- 21. 將字符串拆分爲字符串
- 22. 使用拆分將字符串拆分爲2個字符組?
- 23. 分割字符串使用多字符分隔符
- 24. 使用字符串中存在的分隔符分割字符串
- 25. 分割字符串用加號(+)字符
- 26. 用特殊字符分割字符串
- 27. 分割字符串
- 28. 分割字符串
- 29. 分割字符串
- 30. 分割字符串
有什麼問題?您可能需要查看右側的第一個相關鏈接。 – chris 2013-04-26 18:21:57
我不知道如何拆分字符串來獲得兩個int並忽略// – glethien 2013-04-26 18:22:27
你想用字符串做什麼?任何代碼? – 2013-04-26 18:22:57