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. 分割使用boost ::字符串分割
- 5. 区分字符串中使用字符串分割
- 6. Elisp拆分字符串函数来拆分字符串。字符
- 7. C#拆分字符串 - 将字符串拆分为数组
- 8. 使用C++拆分字符串11
- 9. 字符串分割到字符串
- 10. 字符串分割到字符串
- 11. 将字符串拆分为字符串
- 12. 将字符串拆分为字符串
- 13. 从字符串拆分字符串
- 14. 分割字符串用加号(+)字符
- 15. 分割字符串[]
- 16. 分割字符串
- 17. 分割字符串
- 18. 分割字符串
- 19. 分割字符串
- 20. 分割字符串
- 21. 字符串分割
- 22. 分割字符串
- 23. 分割字符串
- 24. 分割字符串
- 25. 分割字符串
- 26. 使用awk拆分非字母数字字符的字符串
- 27. C分割字符串
- 28. C#分割字符串?
- 29. C++分割字符串
- 30. 分割字符串C++
有什么问题?您可能需要查看右侧的第一个相关链接。 – chris 2013-04-26 18:21:57
我不知道如何拆分字符串来获得两个int并忽略// – glethien 2013-04-26 18:22:27
你想用字符串做什么?任何代码? – 2013-04-26 18:22:57