c++ 에서 string 배열에 관련한 질문입니다. ㅠ

allsoft의 이미지

한 클래스에서 string str[10] 이런식으로 문자열 배열을 잡았습니다.

헌데 문자배열의 문자열에서 문자 하나를 선택해서 다른 인덱스값과 교환할려고 하는데..

도통 안되네요.; 참조가 잘못됫다면서 ㅠ

현재 클래스에서
string& operator [](int index){return TextPicture

    ;}

    이렇게 선언하면 문자열을 참조할수 있는데
    문자 하나를 참조 할려면 어떻게 해야 할까요? ;;
    operator[][] ; 무식하게 이렇게도 시도해봤다는.........................

    예를들어 string 2번째 배열 3번째 캐릭터를 4번째 스트링 배열의 5번째 캐릭터랑 바꾸고 싶어요..

    책 뒤져보고 검색하면서 자료 찾은지 어언 3시간이 다되어 가네요..
    한심한 제 머리를 탓해봅니다. ㅠㅠ

    klyx의 이미지

    무엇을하고 계신겁니까-_-;
    배열은 연산자 오버로딩 안해도 기본으로 []연산이 지원됩니다.
    char temp = str[1][2];
    str[1][2] = str[3][4];
    str[3][4] = temp;

    tinywolf의 이미지

    네.. 윗분 말대로 될 것같은데요..

    class cStrArr
    {
    protected:
    	string str[10];
     
    public:
    	string& operator[](int i)
    	{
    		return str[i];
    	}
    };
     
    int main(int argc, char* argv[])
    {
    	cStrArr test;
     
    	test[1] = "123456";
    	test[3] = "abcdef";
     
    	char temp = test[1][2];
    	test[1][2] = test[3][4];
    	test[3][4] = temp;
     
    	printf(test[1].c_str());
    	printf("\n");
    	printf(test[3].c_str());
    	printf("\n");
     
    	return 0;
    }

    실제로 간단하게 해보니까 잘 되네요..

    ㅡ_ㅡ;

    댓글 달기

    Filtered HTML

    • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
    • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
    • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
    • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

    BBCode

    • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
    • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
    • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
    • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

    Textile

    • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
    • You can use Textile markup to format text.
    • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

    Markdown

    • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
    • Quick Tips:
      • Two or more spaces at a line's end = Line break
      • Double returns = Paragraph
      • *Single asterisks* or _single underscores_ = Emphasis
      • **Double** or __double__ = Strong
      • This is [a link](http://the.link.example.com "The optional title text")
      For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
    • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
    • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

    Plain text

    • HTML 태그를 사용할 수 없습니다.
    • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
    • 줄과 단락은 자동으로 분리됩니다.
    댓글 첨부 파일
    이 댓글에 이미지나 파일을 업로드 합니다.
    파일 크기는 8 MB보다 작아야 합니다.
    허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
    CAPTCHA
    이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.