字符串连接不起作用

时间:2016-11-26 17:32:11

标签: c++ string c++11

我正在尝试连接两个字符串,结果字符串根本没有连接。我不知道问题出在哪里。问题是该行

tempString = tempString+ result;

这是完整的代码

#include <iostream>
#include <cstdio>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <limits>
#include <functional>
#include <algorithm>
#include <cmath>
#include <string>
#include <ostream>
#include <sstream>
#include <bitset>
#include <numeric>
#include <fstream>
#include <stdint.h>


using namespace std;
#define  OTHER 3
#define  SQUARE 1
#define  RECTANGLE 2


static std::string accum(const std::string &s)
{
    string result = "";
    string tempString = "";
    for (size_t i = 0; i < s.length(); i++)
    {
        int temp = i+1;
        if (i == 0)
        {

            result[i] += s[i];
            result[i] = toupper(result[i]);
            tempString = tempString+ result;
            tempString += "-";

        }
        else
        {
            if (i!=1)
            { 
            tempString += '-';
            }
            while (temp > 0)
            {
                tempString+=s[i];

                temp--;
            }

        }
    }

    for (int i = 0; i < tempString.size(); i++)
    {
        if (tempString[i] == '-')
        {
            tempString[i + 1] = toupper(tempString[i + 1]);
        }
    }
    return tempString;
}

int main() {

    string result = accum("abcd");
}

1 个答案:

答案 0 :(得分:0)

更改此行: result[i] += s[i]; 对此:

result += s[i];