Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从800x600中提取800和600_C++ - Fatal编程技术网

C++ 从800x600中提取800和600

C++ 从800x600中提取800和600,c++,C++,我有一个包含解析值的组合框。从800x600到1920x1080。我试图从解析模式中提取(这个)x(这个)。我可以从ComboBox中获得所选的值,但我无法使用我得到的字符串实现我的计划 comboBox->GetSelectedValue() // i get value here std::stringstream buffer; buffer << comboBoxValue; std::string myB

我有一个包含解析值的组合框。从800x600到1920x1080。我试图从解析模式中提取(这个)x(这个)。我可以从ComboBox中获得所选的值,但我无法使用我得到的字符串实现我的计划

        comboBox->GetSelectedValue() // i get value here
        std::stringstream buffer;
        buffer << comboBoxValue;
        std::string myBufferResolution = buffer.str();
        size_t Position1 = 0;
        size_t Position2;

        Position2 = myBufferResolution.find("x", Position1);
        myBufferResolution.substr(Position1, (Position2-Position1));
        Position1 = Position2+1;

        std::cout << "Selected resolution is: " << myBufferResolution << std::endl;
comboBox->GetSelectedValue()//我在这里得到值
std::stringstream缓冲区;
buffer该方法返回一个新分配的字符串,它不会修改正在调用它的对象

因此,它应该是,例如:

const std::string width = myBufferResolution.substr(Position1,
                                                    (Position2-Position1));
我确实认为有更好的方法可以做到这一点(在C中,我会使用
sscanf(myBufferResolution.C_str(),“%ux%u”,&width,&height);

该方法返回一个新分配的字符串,它不会修改正在调用它的对象

因此,它应该是,例如:

const std::string width = myBufferResolution.substr(Position1,
                                                    (Position2-Position1));

我确实认为有更好的方法可以做到这一点(在C语言中,我会使用
sscanf(myBufferResolution.C_str(),“%ux%u”,&width,&height);
。最简单的解决方案是使用正则表达式,但它是 这可能有点过分了;比如:

std::string::const_iterator pivot
        = std::find(resolution.begin(), resolution.end(), 'x');
if (pivot == resolution.end()) {
    //  Error handling, string did not have expected format...
}
std::string width( resolution.begin(), pivot );
std::string height( pivot + 1, resolution.end() );
如果要将这两个值作为字符串

std::istringstream s(resolution);
int width;
int height;
char separ;
s >> width >> separ >> height;
if ( !s || s.get() != EOF || separ != 'x' ) {
    //  Error handling, string did not have expected format...
}

如果您希望将它们作为数值。

最简单的解决方案是使用正则表达式,但它是 这可能有点过分了;比如:

std::string::const_iterator pivot
        = std::find(resolution.begin(), resolution.end(), 'x');
if (pivot == resolution.end()) {
    //  Error handling, string did not have expected format...
}
std::string width( resolution.begin(), pivot );
std::string height( pivot + 1, resolution.end() );
如果要将这两个值作为字符串

std::istringstream s(resolution);
int width;
int height;
char separ;
s >> width >> separ >> height;
if ( !s || s.get() != EOF || separ != 'x' ) {
    //  Error handling, string did not have expected format...
}

如果您希望将它们作为数值。

+1表示
sscanf
。即使在C++中,我也会使用,因为StrueSt流选项太笨拙了。不管怎样,一个好的编译器会保护你免受明显的陷阱。
sscanf
是通往不可维护代码的必经之路。例如,为什么
“%u”
?如果
宽度
高度
的实际类型为
,会发生什么情况?还是在以后的某个时间从C++ >代码> >代码>短/代码>?在C语言中,编译器会检查格式字符串,使其与参数相匹配,所以它并没有那么糟糕。和
%u
在我的世界中,因为分辨率是一个计数值,因此不能是负数。@解绕它不应该是无符号的,因为它们是数值(算术)值,例如类似
abs(h1-h2)
,是有意义的(但使用无符号类型给出错误的结果)。@JamesKanze-Hm。。。那么您什么时候会使用未签名的,那么?+1表示
sscanf
。即使在C++中,我也会使用,因为StrueSt流选项太笨拙了。不管怎样,一个好的编译器会保护你免受明显的陷阱。
sscanf
是通往不可维护代码的必经之路。例如,为什么
“%u”
?如果
宽度
高度
的实际类型为
,会发生什么情况?还是在以后的某个时间从C++ >代码> >代码>短/代码>?在C语言中,编译器会检查格式字符串,使其与参数相匹配,所以它并没有那么糟糕。和
%u
在我的世界中,因为分辨率是一个计数值,因此不能是负数。@解绕它不应该是无符号的,因为它们是数值(算术)值,例如类似
abs(h1-h2)
,是有意义的(但使用无符号类型给出错误的结果)。@JamesKanze-Hm。。。那么,您什么时候会使用无符号变量呢?您真的应该在初始化变量的点定义变量:
size\t Position2=myBufferResolution.find(…
您真的应该在初始化变量的点定义变量:
size\t Position2=myBufferResolution.find(…