如何验证赋值是否在字符串中没有错误?

时间:2014-11-07 05:43:32

标签: c++ string

如果我想验证作业操作怎么办? 链接http://ideone.com/pVE40h

#include <iostream>
#include <string>
using namespace std;

int main() {

    string str;
        int p;
    if  (p = 2)              // 1 case 
        cout << "assigned";
    else
        cout << "not assigned";

    if(str = "my string")   // 2 case 
        cout << "assigned"
    else
        cout << "not assigned";
return 0;
}

我的问题是如果案例1正在运作那么案例2应该如果不是那么为什么呢?它应该那么简单吗? 我知道你们在这里发布的所有内容都试着抓住东西,但我想我应该这样做?

1 个答案:

答案 0 :(得分:0)

try
{
    str = "my_string";
    return 0;
}
catch(...)
{
    return 1;
}