编辑代码

#include <iostream>
#include <tr1/unordered_map>
#include <string>

// 投票函数
void vote(std::tr1::unordered_map<std::string, int>& candidateVotes, const std::string& votedCandidate) {
    // 检查是否已经投过票
    std::tr1::unordered_map<std::string, int>::iterator it = candidateVotes.find(votedCandidate);
    if (it == candidateVotes.end()) {
        // 如果没有投过票,可以增加票数
        candidateVotes[votedCandidate] = 1;
        std::cout << "投票成功!" << std::endl;
    } else {
        // 如果已经投过票,可以进行相应处理(这里简单打印一条消息)
        std::cout << "您已经投过票了,不能重复投票。" << std::endl;
    }
}

int main() {
    // 使用unordered_map来存储候选者和他们的票数
    std::tr1::unordered_map<std::string, int> candidateVotes;

    // 模拟投票过程,这里用字符串表示候选者的名字
    std::string candidateNames[] = {"Candidate1", "Candidate2", "Candidate3", "Candidate4"};

    // 模拟投票
    std::string votedCandidate1 = "Candidate2";
    vote(candidateVotes, votedCandidate1);

    // 模拟投票
    std::string votedCandidate2 = "Candidate2";
    vote(candidateVotes, votedCandidate2);

    return 0;
}