Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

练习10.5 错了 #52

Open
Zhangxh251 opened this issue Aug 10, 2022 · 8 comments
Open

练习10.5 错了 #52

Zhangxh251 opened this issue Aug 10, 2022 · 8 comments

Comments

@Zhangxh251
Copy link

用 c风格的字符串比较 还是会相等的, 比较的还是 字符

@Zhangxh251
Copy link
Author

#include <vector>
#include <list>
#include <string>
#include <iostream>

int main() {
    std::vector<const char*> roster1{ "abc", "def", "ghi" };
    std::list<const char*> roster2{ "abc", "def", "ghi" };
    // Two C-style strings, compare the pointer (addresses of the two strings)
    std::cout << "The two sequences are "
        << (std::equal(roster1.cbegin(), roster1.cend(), roster2.cbegin()) ?
            "equal." : "not equal.") << std::endl;  // not equal

// Two library strings, compare the value
    std::vector<std::string> roster3{ "abc", "def", "ghi" };
    std::list<std::string> roster4{ "abc", "def", "ghi" };
    std::cout << "The two sequences are "
        << (std::equal(roster3.cbegin(), roster3.cend(), roster4.cbegin()) ?
            "equal." : "not equal.") << std::endl;  // equal

// One library string and one C-style string, compare the value
    std::cout << "The two sequences are "
        << (std::equal(roster1.cbegin(), roster1.cend(), roster4.cbegin()) ?
            "equal." : "not equal.") << std::endl;  // equal

    return 0;
}

image

@lhy112233
Copy link

c风格的字符串是不能直接比较的,它只是个地址而已呀

@Zhangxh251
Copy link
Author

可以运行一下,看看结果

@Zhangxh251
Copy link
Author

c风格的字符串是不能直接比较的,它只是个地址而已呀
可以运行一下,看看结果

@Zhangxh251
Copy link
Author

具体我也没想明白

@lhy112233
Copy link

lhy112233 commented Nov 25, 2022 via email

@lhy112233
Copy link

可以运行一下,看看结果

有10.5题吗?发一下页号

@lhy112233
Copy link

lhy112233 commented Nov 25, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants